In this article I will explain with an example, how to get cell value of GridView Row in RowCommand and RowDataBound events in ASP.Net using C# and VB.Net.
HTML Markup
The following HTML Markup consists of an ASP.Net GridView three BoundField columns and a ButtonField column.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Customer Id" ItemStyle-Width="90" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" />
<asp:ButtonField CommandName="Select" Text="Select" ButtonType="Button" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the GridView
The GridView is populated using some dummy records using DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Get cell value of GridView in RowDataBound event in ASP.Net
The row is referenced using the Row property of GridViewRowEventArgs object and using the reference of the GridView Row, the values from the cells are fetched.
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Access Cell values.
int customerId = int.Parse(e.Row.Cells[0].Text);
string name = e.Row.Cells[1].Text;
string country = e.Row.Cells[2].Text;
}
}
VB.Net
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
'Access Cell values.
Dim customerId As Integer = Integer.Parse(e.Row.Cells(0).Text)
Dim name As String = e.Row.Cells(1).Text
Dim country As String = e.Row.Cells(2).Text
End If
End Sub
Get cell value of GridView in RowCommand event in ASP.Net
The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the GridView Row is referenced.
Finally using the reference of the GridView Row, the values from the cells are fetched.
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Determine the RowIndex of the Row whose Button was clicked.
int rowIndex = Convert.ToInt32(e.CommandArgument);
//Reference the GridView Row.
GridViewRow row = GridView1.Rows[rowIndex];
//Access Cell values.
int customerId = int.Parse(row.Cells[0].Text);
string name = row.Cells[1].Text;
string country = row.Cells[2].Text;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer Id: " + customerId + "\\nName: " + name + "\\nCountry: " + country + "');", true);
}
VB.Net
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
'Determine the RowIndex of the Row whose Button was clicked.
Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
'Reference the GridView Row.
Dim row As GridViewRow = GridView1.Rows(rowIndex)
'Access Cell values.
Dim customerId As Integer = Integer.Parse(row.Cells(0).Text)
Dim name As String = row.Cells(1).Text
Dim country As String = row.Cells(2).Text
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer Id: " & customerId & "\nName: " & name & "\nCountry: " & country & "');", True)
End Sub
Demo
Downloads