How to get gridview row id using

In this article I have explained about How to get Gridview rowid using steps I have clearly explained in this article.

In this article I have explained about How to get Gridview rowid using 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";
dtcol1.DataType = System.Type.GetType("System.String");
dtcol1.ColumnName = "ReligionName";
int i;
for (i = 0; i < 4; i++)

dtrow = dtNewTable.NewRow();
dtrow["Religionid"] = i;
dtrow["ReligionName"] = "";

GridView1.DataSource = dtNewTable;



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" "">

<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>

<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderWidth="1px" CellPadding="4" Width="472px" OnRowDataBound="GridView1_RowDataBound">


<asp:TemplateField HeaderText="UserID">

<asp:TextBox ID="TextBox1" Text='<%# Bind("Religionid") %>' runat="server"></asp:TextBox>
<asp:TemplateField HeaderText="UserID">

<asp:TextBox ID="TextBox2" Text='<%# Bind("ReligionName") %>' runat="server"></asp:TextBox>





Author: Shivprasad H23 Jul 2012 Member Level: Bronze   Points : 0

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];

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: