?
Solved

How to retrieve the index of cell clicked in gridview

Posted on 2006-07-04
7
Medium Priority
?
1,131 Views
Last Modified: 2008-01-09
I need to be able to retrieve either the cell index or column index of the header cell in my gridview when the header cell is clicked.  When the user clicks header cell for a particular column, I want to fire code that will copy whatever is in the first row of the clicked column to all the cells below the first row.

Any help will be appreciated.
0
Comment
Question by:rbecker54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 17037658
Turn on sorting, then the header's become links, you on them, catch the sort command, determine which column was clicked, then in the OnItemDataBound event for the grid view, take whatever action you need to. Or if you updating a datasource, then update the data store in the onsortcommand
0
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 17037931
I realize after I typed this that the sort command happens after the databound event so you could either re-databind in the sort command or catch the sort command in the Page_Load event view the Request.Form properties, which requires more work.
0
 

Author Comment

by:rbecker54
ID: 17038644
Could you please post some sample code that implements your suggested solution?
Thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Joeisanerd
ID: 17038676
okay, first you would need to declare a member variable the refers to the column index that was clicked like

    private int colIndex = -1;

then make sure you turned on the Sorting feature of the gridview AllowSorting = true
add the Sorting event and put something like this in there

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
        e.Cancel = true;  // cancel the sort because we don't want it to actually sort
        string selColumn = e.SortExpression; //next grab the sort express which will be the column name
        this.colIndex = dt.Columns[selColumn].Ordinal; // then depending on your setup you would get the column index
        // in my case I created a datatable called dt

       // finally you have to rebind the GridView because the sort command fires after the grid is databound
      GridView1.DataBind();
}        

then in the databound event for the GridView1 simple check to see if the colIndex is > -1

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && this.colIndex > -1)
        {
            // do something, in this case I just set all of the rows for that column Red
            e.Row.Cells[colIndex].BackColor = System.Drawing.Color.Red;
        }

again if you have alot of data, then having the datagrid bound twice might not be so good, so you could get the column index other ways, but it depends on your setup. you could modify the header templates to inlcude an HTML hyperlink that passes the column index as a Query string parameter and grad that in the page load event and still use the same RowDataBound event.
    }
   
   
   
0
 

Author Comment

by:rbecker54
ID: 17041149
Thanks for the example.  I am using a sqldatasource control that runs a stored procedure as the datasource for my gridview and the sqldatasourcecontrol doesn't have a columns collection that I can access as you have your datatable.
0
 
LVL 9

Accepted Solution

by:
Joeisanerd earned 2000 total points
ID: 17042123
since it is coming from an SqlDataSource you probable no the order of the columns so just create a case statement that checsk to see which column number the column name corresponds to

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
        e.Cancel = true;  // cancel the sort because we don't want it to actually sort
        string selColumn = e.SortExpression; //next grab the sort express which will be the column name
       
        switch(selColumn)
        {
              case "MyColumn":  this.colndex = 0; break;
              case "ZipCode" : this.colIndex = 1; break;
              default : this.colIndex = -1; break;
        }

       // finally you have to rebind the GridView because the sort command fires after the grid is databound
      GridView1.DataBind();
}    
0
 

Author Comment

by:rbecker54
ID: 17044042
Thanks Joeisanerd!  This was a big help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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