A Panel Control doesn't appear in a GridView Column - asp.net

one of the GridView Column has to store a Panel Control (with some Controls inside). The problem is, that code doesn't work, I mean, the Panel doesn't appear inside the column.
Panel myPanel = new Panel();
LinkButton zatw = new LinkButton();
zatw.CommandName = "Accept";
zatw.Text = "Accept";
LinkButton odrz = new LinkButton();
odrz.CommandName = "Deny";
odrz.Text = "Deny";
myPanel.Controls.Add(zatw);
myPanel.Controls.Add(odrz);
DataTable DT = new DataTable();
DT.Columns.Add("Options", typeof(Panel));
DataRow myRow = DT.NewRow();
myRow1[0] = myPanel;
DT.Rows.Add(myRow1);
GridView1.DataSource = DT;
GridView1.DataBind();
...

That's because DT.Columns.Add("Options", typeof(Panel)); won't accept a control type as the second argument.
From the documentation. DT.Columns is of type
DataColumnCollection
that, indeed, has a method Add(String, Type) as you used it. But the Type is the column data type... it does not accept a control.
Example:
Private Sub AddColumn()
Dim columns As DataColumnCollection = _
DataSet1.Tables("Orders").Columns
Dim column As DataColumn = columns.Add( _
"Total", System.Type.GetType("System.Decimal"))
column.ReadOnly = True
column.Unique = False
End Sub
In this example, a column of name "Total" and type "decimal" is being created.

I can't really tell what you are trying to do exactly but I don't think you can create a grid out of controls in this manner. Why don't have your grid use a template column and then adjust the template based on the data you bind to instead of binding to a prebuilt UI like you are trying to do?

Related

How to display a single row in asp.net web forms?

I ask myself how I can display a single row from a datatable object in a control like a gridview.
I already did it with label objects like here: (this is in load event. I already have buttons which increment the zero and decrement)
Tbname.Text = (dset.Tables("coduta").Rows(0).Item("Firma"))
TbStraße.Text = (dset.Tables("coduta").Rows(0).Item("Straße_Firma"))
TbHausnummer.Text = (dset.Tables("coduta").Rows(0).Item("Hausnummer_Firma"))
TbOrt.Text = (dset.Tables("coduta").Rows(0).Item("Ort_Firma"))
the point is I want to show the specific row in something like a gridview control. The only Idea i have is, to create a new table out of the row and that looks like a too complicated way for this. I hope guys can help
cheers steven
I am from C# background but this approach should help you.
Get first row from existing table.
Make clone of existing table.
Add that row to clone table.
Assign that table as datasource for grid
DataRow dr = dset.Tables("coduta").Rows(0);
DataTable dtNew = dset.Tables("coduta").Clone();
dtNew.Rows.Add(dr.ItemArray);
grid.DataSource = dtNew;
grid.DataBind();
try
da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.SelectCommand = new SqlCommand(#"SELECT * FROM coduta", connString);
da.Fill(ds, "coduta");
dt = ds.Tables["coduta"];
foreach (DataRow dr in dt.Rows)
{
//here is your row of data
}

Adding row in asp.net table

I have the following code in my .aspx file
<asp:Table ID="tabStudent" runat="server"></asp:Table>
I declared all variables which I used below. I wrote the following code snippet in the page load event. But it only shows only one row which having last row in my database. But when I am using message box inside this while loop to print those values I am getting all the rows.
Reader = Command.ExecuteReader()
While Reader.Read()
lblRollNo.Text = Reader.Item(0)
lblName.Text = Reader.Item(1)
lblDob.Text = Reader.Item(2)
tcRollNo.Controls.Add(lblRollNo)
tcName.Controls.Add(lblName)
tcDob.Controls.Add(lblDob)
TableRow.Cells.Add(tcRollNo)
TableRow.Cells.Add(tcName)
TableRow.Cells.Add(tcDob)
tabStudent.Rows.Add(TableRow)
End While
what is wrong with this code?
You are not creating new controls in the loop but you're always reusing the same.
While Reader.Read()
TableRow = New TableRow()
lblRollNo = New Label()
tcRollNo = New TableCell()
' and so on ...
lblRollNo.Text = Reader.Item(0)
lblName.Text = Reader.Item(1)
lblDob.Text = Reader.Item(2)
tcRollNo.Controls.Add(lblRollNo)
tcName.Controls.Add(lblName)
tcDob.Controls.Add(lblDob)
TableRow.Cells.Add(tcRollNo)
TableRow.Cells.Add(tcName)
TableRow.Cells.Add(tcDob)
tabStudent.Rows.Add(TableRow)
End While

Mutliple dropdowns and values on the Paging

I have a GridView and I populate it via a DATASET . Three of its columns are three DropDownLists and AllowPaging is set to true for the grid view. My problem is when I choose a value on anyone of the ddl and click on serach button i get the data on the gridview which will have paginig, but when i click on the second page i loose teh filtered calue onthe drop down and i again get the earlier dataset.
Is there any way/idea to persist the selected values? Thanks for your help.
Please can you help me with this. if i am not filtering anyone of teh dropdown and then clicking on the second or third page i get the relevant data of that particular page. teh only problem is when i have a value selected on the dropdown .
code:
button click:
{
string _strBU = BUDropDownList.SelectedValue;
string _strOU = OUDropDownList.SelectedValue;
string _strPortalID = !string.IsNullOrEmpty(TxtEmpPortalID.Text.Trim()) ? TxtEmpPortalID.Text.Trim() : string.Empty;
string _strRU = RUDropDownList.SelectedValue;
string _strMngrPortalID = System.Web.HttpContext.Current.User.Identity.Name.ToString();
_strMngrPortalID = _strMngrPortalID.Substring(4, 6);
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "server=;uid=;pwd=;database=HROrgchartDB";
sqlConnection.Open();
SqlCommand sqlEmployeeDetailsCommand = new SqlCommand();
sqlEmployeeDetailsCommand.Connection = sqlConnection;
sqlEmployeeDetailsCommand.CommandText = "EmployeeSearch";
sqlEmployeeDetailsCommand.CommandType = CommandType.StoredProcedure;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#BU", SqlDbType.VarChar, 50)).Value = _strBU;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#OU", SqlDbType.VarChar, 50)).Value = _strOU;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#PORTALID", SqlDbType.VarChar, 6)).Value = _strPortalID;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#RU", SqlDbType.VarChar, 50)).Value = _strRU;
sqlEmployeeDetailsCommand.Parameters.Add(new SqlParameter("#ManagerPortalID", SqlDbType.VarChar, 6)).Value = _strMngrPortalID;
SqlDataAdapter da = new SqlDataAdapter(sqlEmployeeDetailsCommand);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds != null)
{
// gvAddorRelease.Visible = true;
gridReportees.DataSource = ds;
Cache["D2"] = ds;
gridReportees.PageIndex = 0;
gridReportees.DataBind();
}
else
{
}
sqlConnection.Close();
}
}
event for paging :
gridReportees_PageIndexChanging:
{
gridReportees.PageIndex = e.NewPageIndex;
DataSet ds = new DataSet();
ds=(DataSet)Cache["D2"];
gridReportees.DataSource= ds;
gridReportees.DataBind();
}
Problem:
When you click on the next page, dropdown also rebinds due to which there selected index changed to 0.
Solution:
When you press the search button at that time you have to save the selectedvalues of the 3 dropdownlists. the dropdowns are present inside your gridview,so first you have to get the gridview row index. You can get the row index from gridveiw_selectedindexchange event. [Hint]
ViewState["svalue1"] = ((DropDownList) gv.Rows[index].FindControl("dropdownlistID")).Text;
// also get the selected values of other 2 dropdowns
Now after getting the selectedvalues you have to set the dropdwonlist.selected value to the values which we have saved in the ViewState.
gridReportees_PageIndexChanging()
{
// After binding the grid
// in this metho set the dropdown seleted value
// example get a reference of your dropdown
DropDownList ddl1 = (DropDownList) gv.Rows[index].FindControl("dropdownlistID");
ddl.SelectedValue = ViewState["svalue1"].ToString();
// follow the same steps for other 2 dropdownlists
}
Hope that it works for you.

SqlDataSource.Select()? How do I use this? (ASP.net)

I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?
I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.
This puts the result query in to a DataTable.
DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;
foreach (DataRow dr in dt.Rows)
{
// Do something here IE grab the value of the first column
value = dr[0];
}
Repying to last question in comment:
YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")
I was getting crazy trying to do this simple operation:
retrieving data from sqldatasource and put it into variables that I can manipulate.
At the end, Here the working behind code to do this for VB.NET:
Dim DV As New DataView()
Dim DataTable As New DataTable()
Dim SqlDataSource1 As New SqlDataSource()
Dim VALUE As String
SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * from Table"
DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
DataTable = DV.ToTable()
For Each riga As DataRow In DataTable.Rows
VALUE = riga("table_name").ToString
Next
the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.
ENJOY

how to clear off gridview?

I'm creating a dynamic gridview function which will bind different tables from DB into a datatable and then assign the datatable to the gridview! This is how it works, i have a dropdownlist, gridview and a button, the button will fire specific function based on the dropdownlist selection and then gridview will bind the data, my problem is that, when u press the button for the 1st time, the gridview will bind the data from DB, for second time pressing, the gridview will duplicate the data from the data of 1st time pressing! how do i clear off the gridview to avoid data duplication?
Private Sub Login()
sSql = "" & _
"SELECT TYPE, SUBTYPE, LOGTS, ACTION, USERID, STAT1 " & _
"FROM i_LOG " & _
"WHERE TYPE = 'USR' AND SUBTYPE = 'LOG' " & _
"AND CONVERT(VARCHAR(20), LOGTS, 103) >= '" & txtDTFrom.Text & _
"' AND CONVERT(VARCHAR(20), LOGTS, 103) <= '" & txtDTTo.Text & "'"
DT = CreateDataTable(sSql) 'Retrieve from database.
Session(sSesDT) = DT
GVM.DataTable = DT
GVM.GVAddEmptyRow()
Dim seq As New BoundField
Dim Type As New BoundField
Dim SubType As New BoundField
Dim Logts As New BoundField
Dim action As New BoundField
Dim user As New BoundField
Dim status As New BoundField
seq.HeaderText = GVM.DTNumberField
seq.DataField = GVM.DTNumberField
Type.HeaderText = "Type"
Type.DataField = "TYPE"
SubType.HeaderText = "Subtype"
SubType.DataField = "SUBTYPE"
Logts.HeaderText = "Date and Time"
Logts.DataField = "LOGTS"
action.HeaderText = "Action"
action.DataField = "ACTION"
user.HeaderText = "User ID"
user.DataField = "USERID"
status.HeaderText = "Status"
status.DataField = "STAT1"
gv.AutoGenerateColumns = False
gv.Columns.Add(Type)
gv.Columns.Add(SubType)
gv.Columns.Add(Logts)
gv.Columns.Add(action)
gv.Columns.Add(user)
gv.Columns.Add(seq)
gv.Columns.Add(status)
gv.DataSource = Session(sSesDT)
gv.DataBind()
End Sub
Private Overloads Function CreateDataTable(ByVal sSql As String) As DataTable
Dim DA As New OleDb.OleDbDataAdapter
Dim dtDataTbl As New DataTable
Dim dcDataCol As New DataColumn
With DBMgr
.openCnn()
.SQL = sSql
.openRst()
DA.Fill(dtDataTbl, .rst)
End With
'==Adding Columns==
dcDataCol = New DataColumn 'No
With dcDataCol
.DataType = GetType(Int32)
.ColumnName = GVM.DTNumberField
.AutoIncrement = True
'.AllowDBNull = True
End With
dtDataTbl.Columns.Add(dcDataCol)
'**Adding Columns**
Return dtDataTbl
End Function
Sorry, this is in C#, but just call it before you bind any new data and it'll clear any existing data... unless of course the problem is that the DataSource you are assigning has the duplicated data.
gridview.DataSource=null;
gridview.DataBind();
Also, just to note, you are adding every column in every time that procedure is called? You need to run:
gridview.Columns.Clear();
You can reset it's data source and rebind it for example
gv.datasource=""
gv.databind()
and then reuse it
Usually this does not happen when you rebind grid like:
grid.DataSource = 'NEW_DATA_SOURCE'
grid.DataBind
I think may be the problem is in your function fetching data:
CreateDataTable(sSql)
Please check inside this if you are fetching data every time into a new DataTable or using same one (may be declared at global scope or passed from your class to data accessing component). I'd lost whole night once to fix this :(
Place your gridview in a table with an id:
<table id="myTable" border="0">
<tr><td>
<asp:GridView Width="765px" ID="mygrid" runat="server">
...
...
</asp:GridView>
</td></tr></table>
Then call from a button click your javascript function:
function clearGridview()
{
var rows = document.getElementById('myTable').getElementsByTagName('tbody') [0].getElementsByTagName('tr').length;
if(rows!=0)
document.getElementById("myTable").deleteRow(0);
}

Resources