Solved

How to retrieve the index of cell clicked in gridview

Posted on 2006-07-04
7
1,103 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.Net Session State alternatives 3 64
Hidden Field Value 10 63
ASP.net Javascript use multiple sliders 2 20
designing in object programming 12 52
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

19 Experts available now in Live!

Get 1:1 Help Now