• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4430
  • Last Modified:

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


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" />
2 Solutions
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" />

jazz__manAuthor Commented:
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?

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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
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
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now