Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gridview row clickable except for first column?

I'm using the following code to make the entire row of my gridview clickable:

 protected void gridMSDS_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.backgroundColor='#EEFF00'";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor='White'";

            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gridMSDS, "Select$" + e.Row.RowIndex);
        }
    }

Which works great, except now I want to add edit ability to the grid. This works, but when I have both the row clickable and editing functions turned on, clicking the "Edit" link button often fires the row click event and vice versa.

So, how can I keep row clickable, except for specified columns?

UPDATE: Here's what I'm using.

Based on Justin's solution:

 List<int> notClickable = new List<int>();
 {
       notClickable.Add(0);
       notClickable.Add(2);
 }

 for(int i = 0; i < e.Row.Cells.Count; i++)
 {
     if (!notClickable.Contains(i))
     {
          e.Row.Cells[i].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.gridMSDS, "Select$" + e.Row.RowIndex);
     }
 }
like image 350
MAW74656 Avatar asked Apr 25 '11 18:04

MAW74656


1 Answers

The trick is the register the click on the specific columns that need to be clickable. The code below assumes you know the indexes that should be clickable (in this case 0).

e.Row.Cells[0].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.gridMSDS, "Select$" + e.Row.RowIndex);
like image 165
Justin C Avatar answered Oct 27 '22 00:10

Justin C