Retrieving an ASP.Net Gridview rowheader name.

Good Day All,

Is there a way to retrieve an ASP.Net gridview rowheader name at runtime?

I'm populating a Gridview automatically from a SQL stored procedure.

The values returned reflect a figure for a certain activity for a certain day for a certain company. You thus have a column with a list of company names, then (in this case) 29 columns reflecting dates from today until 28 days in the future. Under each date column, you have a figure representing the particular company's performance. The column names reflected today, will differ from the column names reflected tomorrow, hence my problem.

My users have to be able to click on a cell in the Gridview, and then be redirected to another page giving more information regarding the value reflected in the specific cell.

By using the RowDataBound option, I'm able to achieve this, but my problem is that I need to pass the actual header name (the date) of the cell clicked, as a variable.

Any ideas would be appreciated.
protected void GriView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
               int x = 29;
                for (int i = 1; i <= x; i++)
                {
			string headername;
 
			// >> need to extract the headername here (??)
     
                    e.Row.Cells[i].Attributes.Add("onclick", "TestCheck(" + e.Row.Cells[i].Text.ToString() + "," + headername")");   
                }
     
        }

Open in new window

vniekewjAsked:
Who is Participating?
 
abelCommented:
Btw, if your cells contain something clickable, and the whole thing is databound, you can also add a linkbutton or similar inside the cell and fill the attributes of it with the right data. Then you can use normal code-behind (unless of course you need this javascript).

In your case, "i" contains the column index and apparently you are skipping the first. What you can change your code to is:

for (int i = 1; i <= x; i++)
{
    string headername = GridView1.Columns[i].HeaderText;
     
    e.Row.Cells[i].Attributes.Add("onclick", "TestCheck(" + e.Row.Cells[i].Text.ToString() + "," + headername")");   
}

Open in new window

0
 
abelCommented:
Aren't you looking for something similar to:

GridView1.Columns(i).HeaderText

?
0
 
abelCommented:
(my first post was accidentally in VB style, sorry about that)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
wickedlysmartCommented:
this should help. replace GridView1 with your gridview id

string headername = GridView1.Columns[i].HeaderText;
0
 
vniekewjAuthor Commented:
Thanx for the replies, but now I'm stuck at a new hurdle:

"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

For the life of me, I can't work around this. (Does everybody go through a "moron" phase?)
0
 
abelCommented:
The index you are using was one-based. The index of the columns (or cells) is zero-based. may that be the problem?

You say x = 29. Do you have 29 columns? Then start at 0 and go to 28.
0
 
vniekewjAuthor Commented:
Thanx Abel. I was able to use this info in a different app.
0
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.

All Courses

From novice to tech pro — start learning today.