plomp
plomp

Reputation: 1

Find a GridView row programmatically

The row has a LinkButton that when clicked needs to highlight the row.

Code so far:

protected void linkbutton1_Click(object sender, EventArgs e)
{
    LinkButton l = (LinkButton)sender;
    GridViewRow g = (GridViewRow)l.Parent; // what is the correct way to do this?
    //g.Style etc etc
}

Upvotes: 0

Views: 2069

Answers (3)

Mitul
Mitul

Reputation: 9854

1.) Set Command Name Property to "Select"

2.) Change style either on code behind as shown by @Raymond or give set Cssclass attribute for SelectedRowStyle of gridview to CssClass="selecterowstyle"

.selectedRowstyle { background-color:#EAEAEA; }

Upvotes: 0

Raymond Morphy
Raymond Morphy

Reputation: 2526

first of all set the "CommandName" property of LinkButton to "select", then in the selectedIndexChanging event of gridview write below code:

for (int i = 0; i < GridView1.Rows.Count;i++ )
            GridView1.Rows[i].BackColor = System.Drawing.Color.White;
 GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Cornsilk;

Upvotes: 1

Curtis
Curtis

Reputation: 103348

Make use of the RowCommand event of the GridView instead of the Click event of the LinkButton.

Then you can have a CommandName on the LinkButton such as "HighlightRow" and do something like the following:

Select Case e.CommandName
  Case "HighlightRow"
    e.item.row.attributes("class") = "highlight"
End Select

Sorry its in VB.NET and not C#

Upvotes: 0

Related Questions