How to add ID to GridView rows (IDs should be rendered)?
I am using .NET (C#). I have GridView control.
I have some javascript functions that are manipulating table rows, but it is necessary to have IDs for those rows:
<table>
<tr id=1> ...
<tr id=2> ... //id should come from database
..
My GridView is genereted from Data from DataBase. It is important not to have FAKE ROW IDS, but really row ids from DB (there are some ajax javascript function that updates DB based on those IDs and user manipulations with table).
Part of my GridView is the following:
<asp:GridView ID="grdNews" runat="server" BorderStyle="None" RowStyle-BorderStyle="None"
GridLines="None" CssClass="table" Style="white-space: nowrap" AutoGenerateColumns="False"
DataKeyNames="ID" AllowSorting="True" AllowPaging="true" OnSorting="grdNews_Sorting" OnRowDataBound="grdNews_RowDataBound">
<RowStyle BorderStyle="None" />
<HeaderStyle CssClass="nodrag" />
<Columns>
....
I have tried the following:
protected void grdNews_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.ID = grdNews.DataKeys[e.Row.RowIndex].Value.ToString();
}
}
This gives e.Row.ID the correct value, but this doesn't render this ID.
So, how to render IDs from DataBase for Rows in GridView?
Try following....
protected void grdNews_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = e.Row;
row.Attributes["id"] =grdNews.DataKeys[e.Row.RowIndex].Value.ToString();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With