How to change the color of the row in GridView OnMouseClick

Posted on 2007-07-31
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
    LVL 13

    Expert Comment

    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

    see whether the following example can work for you..

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

    Author Comment

    I really need mouseclick
    LVL 12

    Accepted Solution

    I think this example should work the way you want...

    Author Comment

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

    Expert Comment

    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

    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", "'dee0aa'");

    e.Row.Attributes.Add("onmouseout", "'ebeccd'");


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now