[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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

0
vniekewj
Asked:
vniekewj
  • 4
  • 2
1 Solution
 
abelCommented:
Aren't you looking for something similar to:

GridView1.Columns(i).HeaderText

?
0
 
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:
(my first post was accidentally in VB style, sorry about that)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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