Solved

How to retrieve the index of cell clicked in gridview

Posted on 2006-07-04
7
1,095 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
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:Joeisanerd
Comment Utility
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
Comment Utility
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
Comment Utility
Could you please post some sample code that implements your suggested solution?
Thanks.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:Joeisanerd
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Thanks Joeisanerd!  This was a big help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now