Solved

How to retrieve the index of cell clicked in gridview

Posted on 2006-07-04
7
1,108 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#, asp.net MVC, sql server, LINQ 3 38
ASP Repeater control - onl;y show if flag is true 7 16
Change to file doesn't show up 16 54
Please explain purpose of GZIP 4 36
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

829 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