Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add ID to GridView Row

Tags:

asp.net

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?

like image 967
renathy Avatar asked Oct 18 '12 09:10

renathy


1 Answers

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();


      }
 } 
like image 182
Amol Kolekar Avatar answered Sep 19 '22 23:09

Amol Kolekar