Learn how to a build a cloud-first strategyRegister Now


How to change the color of the row in GridView OnMouseClick

Posted on 2007-07-31
Medium Priority
Last Modified: 2008-01-09
Hello experts,
How to change the color of the row in GridView OnMouseClick (Visual C# ASP.NET web application)
I implemented this example:
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.

Question by:itproem
  • 2
  • 2
  • 2
  • +1
LVL 13

Expert Comment

ID: 19600327
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.
LVL 12

Expert Comment

ID: 19600351
see whether the following example can work for you..

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


Author Comment

ID: 19600410
I really need mouseclick
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Accepted Solution

Pra4444 earned 1500 total points
ID: 19600460
I think this example should work the way you want...


Author Comment

ID: 19600489
If I'll use SelectedRowStyle-xxx in XYX procedure, should I then use OnSelectedIndexChanged ="XYX" ?
LVL 13

Expert Comment

ID: 19600512
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)
LVL 14

Expert Comment

ID: 19600738
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'");


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

864 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