Solved

How to retrieve the index of cell clicked in gridview

Posted on 2006-07-04
7
1,105 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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 …

772 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