• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

How to access gridview DataRow field to conditionally set Gridview Header field

I have a gridview.  One of my dataRow fields holds either an "H" or a "D" value to indicate "Hours" or "Days".    I want to display "Hours" or "Days" in the header based on the dataRow field value.

Is there a way to determine the value of the dataRow field when the gridview is processing the Header row?  

 if (e.Row.RowType == DataControlRowType.Header)
                if (e.Row.Cells[2].Text.Contains("H"))  This is the datarow field value I'm trying to access (this doesn't work)

1 Solution
1. create an OnDataBound event for the gridview.
2. Then, assuming all no row can have an "H" when another row contains a "D", look at the first row
3. If the first row has an "H" change the gridview header row column
4. Do the same if the row has a "D"
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, assuming that your GridView DataSource is a DataTable object I did this example for you:
<%@ Import Namespace="System.Data" %>

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Random rnd = new Random();

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            // Default show hours data.
            Button1_Click(this, e);

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.Header)
            DataTable dtt = GridView1.DataSource as DataTable;
            if (dtt != null && dtt.Rows.Count > 0)
                if (dtt.Rows[0]["ColumnB"].ToString().Equals("H"))
                    e.Row.Cells[0].Text = "Hours";
                    e.Row.Cells[0].Text = "Days";


    protected void Button1_Click(object sender, EventArgs e)
        // Show hours data.

    protected void Button2_Click(object sender, EventArgs e)
        // Show days data.

    void BindMyData(bool hours)
        DataTable dtt = new DataTable();
        dtt.Columns.Add("ColumnA", typeof(int));
        dtt.Columns.Add("ColumnB", typeof(string));

        for (int i = 0; i < 12; i++)
            dtt.Rows.Add(rnd.Next(1, 120), hours ? "H" : "D");

        GridView1.DataSource = dtt;

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                <asp:BoundField DataField="ColumnA" />
                <asp:BoundField DataField="ColumnB" />
        <asp:Button ID="Button1" runat="server" Text="Show Hours Data" OnClick="Button1_Click" />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Show Days Data" OnClick="Button2_Click" />
        <br />

Open in new window

You can download the full aspx file from this link:

I hope this helps.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now