Home > Forum > ASP.NET > How to Find Table Row Id in GridView

How to Find Table Row Id in GridView

by Thamil   on Jul 03, 2013   Category: ASP.NET  |  Views: 8684    |  Points: 5   |  Gold 
  Reply
how to find out the table row id in gridview

<asp:TemplateField>
<ItemTemplate>
<table>
<tr id="set0" runat="server">
<th>
<asp:Label runat="server" ID="Label19" Style="text-transform: uppercase font-size: 16px float: left" Text="EMI SET NO"></asp:Label>
</tr>
</table>

</ItemTemplate>
</asp:TemplateField>

question posted on behalf of- santosh Deshmukh

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :How to Find Table Row Id in GridView   
by Thamil
on Jul 3 2013 10:23AM
Points : 10
Gold 
Use GridView1_RowDataBound event in gridview to get table rows id values, see the below sample.

Gridview:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" 
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Test Heading">
<ItemTemplate>
<table>
<tr id="set0" runat="server">
<td>
<asp:Label ID="lblCustID" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Place") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Code behind part

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Place", typeof(string));

DataRow rw1 = dt.NewRow();
rw1[0] = "test1";
rw1[1] = "test1";
dt.Rows.Add(rw1);

DataRow rw2 = dt.NewRow();
rw2[0] = "test2";
rw2[1] = "test2";
dt.Rows.Add(rw2);

DataRow rw3 = dt.NewRow();
rw3[0] = "tes3";
rw3[1] = "test3";
dt.Rows.Add(rw3);

return dt;

}

Rowdatabound event to get id and assign values
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HtmlTableRow rw = (HtmlTableRow)e.Row.FindControl("set0");
rw.BgColor = "Green";
}
}


I hope this will help you.

Recent Post

Latest Posts