SelectedRowStyle not working on GridView control - I simply want to highlight the selected row, but its not working!

Posted on 2007-07-30
Last Modified: 2013-12-17

Could someone please advise me on the selction of a row on the following gridview control.
When I select a row on the grid I want the row colour to change, I have done this before and it is my understanding that this sort of thing is usually very easy on a grid view control, however it is proving to be a bit of a nuisance.
I have tried setting the background colour of the SelectedRowStyle of the grid and also tried applying the colour using CSS, but nothing seems to be working. Is there something obvious I am doing wrong?

Thanks in advance.
<asp:GridView ID="gvTest"  runat="server" CssClass="GridFormat" AutoGenerateColumns="False" GridLines="None" DataKeyNames="MyID" >
                        <HeaderStyle CssClass="GridHeader"/>
                        <asp:BoundField DataField="MyID" Visible="False" HeaderText="MyID" />
                            <asp:BoundField DataField="Head1" HeaderText="Head1" />
                            <asp:BoundField DataField="Head2" HeaderText="Head2" />
                            <asp:BoundField DataField="Head3" HeaderText="Head3" />
                            <asp:BoundField DataField="Head4" HeaderText="Head4" />
                            <asp:TemplateField ShowHeader="False">
                                    <asp:ImageButton ID="btnMyImageButton" runat="server"                               ImageUrl="../Images/ButtonImage.gif" CausesValidation="False"                               CommandName="Select" />
                                <ItemStyle HorizontalAlign="Right" />
Question by:jazz__man
    LVL 14

    Expert Comment

    LVL 14

    Accepted Solution

    You can find couple of more solutions here.

    Here is my sample gridview. Works perfect.

     <asp:GridView id="GridView1"  Runat="Server" AutoGenerateColumns="false"  DataKeyNames="ID"  AllowPaging="False"  Width="800px"  SelectedRowStyle-BackColor="#c2d0d9"
                    HeaderStyle-BackColor="#336699"  HeaderStyle-Font-Bold="True" Style="float:left" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >


               <asp:TemplateField HeaderText="ProjectId">
                    <asp:LinkButton ID="LinkButton1"  Text=""  ForeColor="#336699"  CommandName="Select" Runat="Server">
                        <%# Eval("ID") %>
                 <HeaderStyle Width="50px" />
                  <asp:BoundField HeaderText="Name"  DataField="ProjectName" />
                  <asp:BoundField HeaderText="Priority" DataField="ProjectPriority" />
                       <SelectedRowStyle BackColor="#C0D2DD" />
                       <HeaderStyle BackColor="#C0D2DD" Font-Bold="True" />

    LVL 2

    Author Comment

    I have already tried the above suggestion but it still does not seem to be working.

    I am using a generic class list object as the gridViews datasource...could this be anything to do with it?

    LVL 4

    Assisted Solution

    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim CB As CheckBox
            CB = CType(sender, CheckBox)

            If CB.Checked = True Then
                //I'm doing a cast on CheckBox parent to GridViewRow and then setting its color, do this in VB
                ((GridViewRow)CB.parent).BackColor = System.Drawing.Color.Red; //C# code, you must fit to VB
            ElseIf CB.Checked = False Then
               //I'm doing a cast on CheckBox parent to GridViewRow and then setting its color, do this in VB
               ((GridViewRow)CB.parent).BackColor = System.Drawing.Color.White; //C# code, you must fit to VB
            End If
        End Sub
    LVL 14

    Expert Comment

    I don't think it has any thing to do with this.

    Do you have any other CSS contoroling controls or tables having different CSS class than you gridview's css class?

    That can also be a problem
    LVL 4

    Expert Comment

    For anyone else who came across this article on Google, my solution was CSS, the style was being added to the TR tag but the TD was not inheriting it. So I created a class called "selected":

    <SelectedRowStyle CssClass="selected" />

    .selected,.selected TD {background-color:#E0E0E0}

    Note the ".selected, .selected TD", that tells the browser to apply the bg color to any TDs whose parent is .selected.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    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…

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now