?
Solved

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

Posted on 2007-07-30
7
Medium Priority
?
4,230 Views
Last Modified: 2013-12-17
Hi,

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"/>
                        <Columns>
                        <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">
                                <ItemTemplate>
                                    <asp:ImageButton ID="btnMyImageButton" runat="server"                               ImageUrl="../Images/ButtonImage.gif" CausesValidation="False"                               CommandName="Select" />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Right" />
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
0
Comment
Question by:jazz__man
6 Comments
 
LVL 14

Expert Comment

by:chinu1310
ID: 19592534
0
 
LVL 14

Accepted Solution

by:
chinu1310 earned 1600 total points
ID: 19592601
You can find couple of more solutions here.
http://www.webdeveloper.com/forum/archive/index.php/t-106982.html

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" >

            <Columns>

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

            </asp:GridView>
0
 
LVL 2

Author Comment

by:jazz__man
ID: 19592608
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?

0
Technology Partners: 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 4

Assisted Solution

by:Neotk
Neotk earned 400 total points
ID: 19592639
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
0
 
LVL 14

Expert Comment

by:chinu1310
ID: 19592680
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
0
 
LVL 4

Expert Comment

by:timmyt851
ID: 25530737
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" />

CSS:
.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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 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