How to bind Gridview by column index instead of by column ID? - c#

Normally binding data to gridview column is like this:
Text='<%# Bind("regionName") %>'
How do I bind by index instead? For example:
Text='<%# Bind(1) %>'
The reason is because I am using a 2D list for datasource, and 2D list don't have column names.

I used the following for List<string[]> and it worked inside repeater, not sure about gridview:
Text='<%# Eval("[1]")%>'

Related

Create Dynamic ItemTemplate based on GUID column names

I have a process that pivots data to create columns based on the UniqueIdentifier key value. The resulting data table has column names such as:
> PartNum
> [DB1A6498-7CC6-4EA0-846A-9B6EAB771777]
The portion of ASP is:
<asp:TemplateField HeaderText="Part No.">
<ItemTemplate>
<asp:Label ID="lblPartNum" runat="server" Text='<%#Eval("PartNum") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Out">
<ItemTemplate>
<asp:Label ID="lblRunOut" runat="server" Text='<%#Eval("DB1A6498-7CC6-4EA0-846A-9B6EAB771777") %>' />
</ItemTemplate>
</asp:TemplateField>
The Part Number displays just fine, however field based on the uniqueID results in a message:
DB1A6498-7CC6-4EA0-846A-9B6EAB771777/ is neither a DataColumn nor a DataRelation for table .
I read through the data table columns to create the grid columns, which display fine, however when attemtping to dynamically create the ItemTemplate, the error occurs. Putting the brackets "[]" around the uniqueID in the label did not work either.
Any suggestions are greatly appreciated. Thanks
Kevin...your response led me down the path of the brackets. Based on that, I found information about the DataBinder with the final result being that the Text property of the asp:label reads:
Text='<%# DataBinder.GetPropertyValue(Container.DataItem,"[DB1A6498-7CC6-4EA0-846A-9B6EAB771777]") %>'
This is displaying data from the uniqueID fields with no errors.

Dynamic textbox ID in .aspx.cs

I have a question on C#. I use the GridView ItemTemplate to add a textbox to a whole column. I add the ID to the drop down list in ItemTemplate. Therefore, the generated ID of drop down list is 'GridViewID_dropdownListID_number' in each row when I launch the project.
However, I cannot set the drop down list to .Visible = true and .Visible = false in .aspx.cs file. I try to type the 'dropdownListID' and 'GridViewID_dropdownListID_number' to 'Page_Load' function. However, it displays the error message which is under light the statement.
'The name 'GridViewID_dropdownListID_0' does not exist in the current content'
Can I set the drop down list visible to true and false in .aspx.cs?
P.S I can retrieve the row number by GridViewRow
you can use FindControl
DropdownLIst tvSeries = (DropdownLIst)tableOfTVSeries.Rows[0].Cells[2].FindControl("tvSeriesTableCategoryDropdownLIst");
Here is an example how to do this in the item template of a repeater -- this is typically how this problem is solved:
<asp:DataList Runat="server" ...>
<ItemTemplate>
<asp:Label runat="Server" Text='<%# Container.DataItem("data") %>'
Visible='<%# Container.DataItem("makevisible") %>'/>
</ItemTemplate>
</asp:DataList>

How to pass codebehind value to HeaderTemplate label control of Gridview UI

In ASP.NET usign C#, .NET3.5 F/W
Hi,
I've a Gridview with template columns containing textbox and Label controls.
By code through some queries and stored procedures i got some data to a datatable. Now i want to pass this datatable column field or names to the Gridview Label controls.
How can i set the datatable column header names to gridview label control of Header Template?
It's called Binding here is link
<asp:TemplateColumn>
<ItemTemplate HeaderText="PersonId">
<asp:Label id=Label1 runat="server"
Text='<%# Eval("NameOfColumnt") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
Try this
<asp:TemplateColumn >
<ItemTemplate>
<asp:Label id=Label1 runat="server"
Text='<%# DataBinder.Eval(Container,EmployeeName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
For this to work you will have to give your grid view a data source:
yourGridView.DataSource = yourDataTable;
yourGridView.DataBind();
After you do this, you just have to link each control in template columns to column in datatable, for example:
<HeaderTemplate>SomeText</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="a" Text='<%# Eval("nameOfTheColumn") %>' runat="server"></asp:Label>
</ItemTemplate>
EDIT:
Regarding your other question, I suggest you to add bound fields to your grid view dynamically rather then right away in the aspx. Something like this:
foreach (DataColumn column in dt.Columns)
{
BoundField nameColumn = new BoundField();
nameColumn.DataField = column.ColumnName.ToString();
nameColumn.SortExpression = column.ColumnName.ToString();
nameColumn.HeaderText = column.ColumnName.ToString();
gridView.Columns.Add(nameColumn);
}
This way you don't have to know exactly how many columns you have, and have the ability to name them as they are in the data table.

how to bind hash table to gridview

hi i have a hash table with following set of values
int ID 1
string Name ram
list date 2/3/2011
5/3/2011
Code:
<asp:TemplateField HeaderText="ID">
<ItemStyle Width="200px"> </ItemStyle>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
its throws error in bind statement
You need to add a Repeator or GridView or Datalist control to bind the list (dates) inside the ItemTemplate of GridView.
Hash tables have keys and values so you can't bind your grid using the key names (as you are doing) because the key names are not properties of the hash table. If anything, you could have columns bound to Key and Value; however, your data has a List of things (dates) and the Gridview won't know how to bind this list to one column. As AVD suggested, you need to have an item template and inside it, have another gridview if you want to bind a data structure like this to a grid view.

Multiple DataTable columns stacked horizontally in one GridView Column

How can multiple columns from a DataTable be stacked into one column of a GridView ?
Is there a better asp control for performing such an operation?
Lets assume you are binding that value to a label, then it will simply be:
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("FirstName") + Eval("LastName")%>'></asp:Label>
</ItemTemplate>
You could iterate over the columns and rows on the server, pushing the items into any control that will display this one column (list, one column HTML table etc.)

Categories

Resources