Get GridView cell value in RowDataBound and RowCommand events in ASP.Net with C#

Introduction

This article explains how to get cell value of GridView Row in RowCommand and RowDataBound events in ASP.Net using C# and VB.Net.

HTML

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

 Binding the GridView

The GridView is populated using some dummy records using DataTable.

C# Code 

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", "United States");
        dt.Rows.Add(2, "Vepsh", "India");
        dt.Rows.Add(3, "ABC", "France");
        dt.Rows.Add(4, "XYZ", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

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# Code 

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

 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# Code 

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);
}
Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

2 comments: