How to get gridview row id using asp.net
In this article I have explained about How to get Gridview rowid using asp.net.Each steps I have clearly explained in this article.
In this article I have explained about How to get Gridview rowid using asp.net.Each steps I have clearly explained in this article. Just drag and drop one grid view control from tool box and place two text box control within template column.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataRow dtrow;
DataTable dtNewTable;
dtNewTable = new DataTable();
DataColumn dtcol, dtcol1;
dtcol = new DataColumn();
dtcol1 = new DataColumn();
dtcol.DataType = System.Type.GetType("System.Int32");
dtcol.ColumnName = "Religionid";
dtNewTable.Columns.Add(dtcol);
dtcol1.DataType = System.Type.GetType("System.String");
dtcol1.ColumnName = "ReligionName";
dtNewTable.Columns.Add(dtcol1);
int i;
for (i = 0; i < 4; i++)
{
dtrow = dtNewTable.NewRow();
dtrow["Religionid"] = i;
dtrow["ReligionName"] = "";
dtNewTable.Rows.Add(dtrow);
GridView1.DataSource = dtNewTable;
GridView1.DataBind();
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView vDataRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = "javascript:alert('RowID= "+e.Row.RowIndex.ToString()+"')";
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderWidth="1px" CellPadding="4" Width="472px" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="UserID">
<ItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%# Bind("Religionid") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserID">
<ItemTemplate>
<asp:TextBox ID="TextBox2" Text='<%# Bind("ReligionName") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
If you are looking to access row on gridview row command then here is the code,
int rowIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow grow = grvXYZ.Rows[rowIndex];