Friday, July 10, 2015

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

4 comments:


  1. Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students. .Net Training in Chennai

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

      Delete
  2. I like your writing style, it was very clear to understanding the concept well; I hope you ll keep your blog as updated.
    Regards,
    PHP Training in Chennai|PHP Course in Chennai|web design training in chennai

    ReplyDelete