how to bind hash table to gridview - c#

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.

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.

Bind label text inside gridview

I saw few grid view examples where data is bound to grid view from a sql or other database. The question is -
<ItemTemplate>
<asp:Label ID="label1" runat="server" Text='<%#Bind("name") %>'>
</asp:Label>
</ItemTemplate>
How is Text='<%#Bind("name")%>' working? From where does the label gets the text?
I am using mysql I have a drop down list of tables, and a button. Whenever the user selects any table from ddl, and clicks on the button, I will bind the selected table with the grid.
I have enable autogenerating=true for edit and delete buttons.
I will write code for that, but whenever the user selects a different table will the grid show edit and delete buttons? and what about the Bind("value") ? will it change for every table?
Might be a silly question but please help!
The #Bind("name") command will insert the value of the column named name from whichever table you're binding to the GridView. Therefore, each of your tables would need a column named name for this label to be populated.
Also, #Bind should be used for both displaying and updating data. If you only need to display data, #Eval("name") is a better choice, as this is read-only.
you have to use the #Eval, for example,
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("Username") %>'/>
</EditItemTemplate>

Hyperlink in Gridview which is autogenerated in C#

I have a Gridview which is autogenerated by fetching data from various tables.
Now my requirement is i need to make my first column as hyperlink so that if it is clicked then it has to navigate to another page with hyperlinked value.
In the second page i have Lable this label should show the hyperlink value and based on that the data will be populated on the second page.
For example:
I have a gridview with 10 columns..My first column is Emp Id.
If that id is clicked it has to take me to second page and the label control must get this id value and based on that id the rest of the info like emp name emp DOB should be filled.
i am using C# as my code behind.
Can anyone help me to proceed..
Awaiting ur reply
You need the <asp:HyperLinkField />
<asp:GridView>
<Columns>
<asp:HyperLinkField HeaderText="Id" DataTextField="YourID" DataNavigateUrlFields="YourID"
DataNavigateUrlFormatString="SecondPage.aspx?Id={0}" />
</Columns>
</asp:GridView>
You might need to remove the AutoGenerateColumns="true" property and type the fields yourself, selecting only the columns you want to show - using <asp:BoundFied DataField="ColumnName" />
In case you want to pass multiple values in the querystring, separate the fields with comma
DataNavigateUrlFields="YourID, SecondField"
and your format string would be
DataNavigateUrlFormatString="SecondPage.aspx?Id={0}&param2={1}"
Other links
HyperLinkField.DataNavigateUrlFormatString Property
How to pass multiple values in HyperlinkField
You can use a TemplateField column in your GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="linkToDetails" runat="server" NavigateUrl='Details.aspx?empId=<%# Eval("empId") %>' Text='<%# Eval("empId") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
On the Details.aspx page you should get the empId from the QueryString and get the details from the database.

How to bind Gridview by column index instead of by column ID?

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]")%>'

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