Solved

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

Posted on 2011-09-06
2
505 Views
Last Modified: 2013-11-26
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)

0
Comment
Question by:ppeterso
2 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36491146
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"
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 250 total points
ID: 36491772
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";
                else
                    e.Row.Cells[0].Text = "Days";

                return;
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        // Show hours data.
        BindMyData(true);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        // Show days data.
        BindMyData(false);
    }

    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;
        GridView1.DataBind();
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="ColumnA" />
                <asp:BoundField DataField="ColumnB" />
            </Columns>
        </asp:GridView>
        <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 />
    </div>
    </form>
</body>
</html>

Open in new window


You can download the full aspx file from this link:
http://dl.dropbox.com/u/13237304/EE/CustomGridView.zip

I hope this helps.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question