Link to home
Start Free TrialLog in
Avatar of itproem
itproem

asked on

How to change the color of the row in GridView OnMouseClick

Hello experts,
How to change the color of the row in GridView OnMouseClick (Visual C# ASP.NET web application)
I implemented this example:
http://gridviewguy.com/ArticleDetails.aspx?articleID=174
and  it's working, but in this example row changes the color on click,
and it turns back to default color when you click again.
But I need this scenario: when you click/select one row1 it will become yellow (in this case),
and after clicking another row2, the previous selected row1 will turn back to default color without clicking it again, and of course, the new clicked row2 will become yellow. So, only one row can be colored at the same time because I want to see which row is selected.

Thanks
Avatar of joechina
joechina

In your case, the best way is to set

SelectedRowStyle-xxx properties of GridView. (I would suggest you to use css or skin file)

Then in your code, just specify which row is current selected.
SelectedIndex or SelectedRow property could be used.
see whether the following example can work for you..

do you want only mouseclick or will mouseover and mouseout work for you??

https://www.experts-exchange.com/questions/22690077/Mouseover-hyperlink-effect-on-GridView-row.html
Avatar of itproem

ASKER

I really need mouseclick
ASKER CERTIFIED SOLUTION
Avatar of Pra4444
Pra4444
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itproem

ASKER

If I'll use SelectedRowStyle-xxx in XYX procedure, should I then use OnSelectedIndexChanged ="XYX" ?
No.
Just in your rowcommand event or mouseclick event specify

yourGridView.SelectedIndex = Convert.ToInt32(e.CommandArgument);
(or something like it depends on the event)  (e.CommandArgument is from rowcommand event)
Static and rollover effect code

Statically you can define in your gridview.


 <SelectedRowStyle BackColor="#C0D2DD" />

For rollover color change do like this.

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{



if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='dee0aa'");

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='ebeccd'");


}
}