In this article I will explain how to dynamically change (modify) GridView Cell value using RowDataBound event in ASP.Net using C# and VB.Net.
 
 
Concept
In database tables, we have some specific codes which have specific meaning. Programmers are aware of the significance of these codes but end users won’t understand what a particular code means. Hence using RowDataBound event, we can dynamically change the code value to some meaningful and easy to understand words.
For example, consider a scenario of Meeting attendance. The Status column in database consists of two values A and P where A means Absent i.e. the person has not attended meeting and P means Present i.e. the person has attended the meeting.
Now end users won’t understand what is A and P and hence using RowDataBound event, A will be replaced with Absent and P will be replaced with Present which now makes it more meaningful.
 
 
HTML Markup
The following HTML markup consists of an ASP.Net GridView consisting of three BoundField columns.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="100" />
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
I have created a dynamic DataTable with some dummy data and it is used to populate the GridView control in the Page Load event.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net
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("Status") });
        dt.Rows.Add(1, "John Hammond", "A");
        dt.Rows.Add(2, "Mudassar Khan", "P");
        dt.Rows.Add(3, "Suzanne Mathews", "P");
        dt.Rows.Add(4, "Robert Schidner", "A");
        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("Status")})
        dt.Rows.Add(1, "John Hammond", "A")
        dt.Rows.Add(2, "Mudassar Khan", "P")
        dt.Rows.Add(3, "Suzanne Mathews", "P")
        dt.Rows.Add(4, "Robert Schidner", "A")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Dynamically change GridView Cell value using RowDataBound event
Inside the OnRowDataBound event handler, the value of the Status column is fetched and then compared. If the value is A then it is set to Absent and if the value is P then it is set to Present.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TableCell statusCell = e.Row.Cells[2];
        if (statusCell.Text == "A")
        {
            statusCell.Text = "Absent";
        }
        if (statusCell.Text == "P")
        {
            statusCell.Text = "Present";
        }
    }
}
 
VB.Net
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim statusCell As TableCell = e.Row.Cells(2)
        If statusCell.Text = "A" Then
            statusCell.Text = "Absent"
        End If
        If statusCell.Text = "P" Then
            statusCell.Text = "Present"
        End If
    End If
End Sub
 
 
Screenshot
Dynamically change GridView Cell value using RowDataBound event in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads