Selecting a row from gridview by clicking anywhere on the row

Posted on 2006-05-30
Last Modified: 2011-10-03
I have a requirement to Select a row from gridview by clicking anywhere on the row. Can anyone give me the code for it.

Question by:de1209
    LVL 19

    Accepted Solution

    You could use: ( but you would also need to disable event validation )

    <% @ Page Language="VB" EnableEventValidation="false"%>

    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
          e.Row.Attributes.Add("onclick", "javascript:__doPostBack('GridView1','select$" & e.Row.RowIndex & "')")
    End If

    End Sub

    Hope that helps,


    Author Comment

    My ButtonField column has Select Button - which is in invisible mode. I need to highlight the row with some color when clicked and change the color of the row when the mouse is over it.
    Also I am using a master page.
    can i add the attributes onmouseover and onclick to the Rowcreated .

    Thanks for your help Max

    Author Comment

    This actually my code

    <asp:GridView ID="gvSelectAddress" runat="server" AllowSorting="True" BackColor="#EEEEEE"  Height="10px"
                        HorizontalAlign="Center"  SelectedRowStyle-BackColor="Magenta"
                        TabIndex="3" Width="776px">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
                            <asp:ButtonField ButtonType="Button"  Text="Select"  CommandName="Select" Visible="true"  />
                        <SelectedRowStyle BackColor="Magenta" />

    In code behind

    Protected Sub gvSelectAddress_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSelectAddress.RowCreated

            If e.Row.RowType = ListItemType.Item Or e.Row.RowType = ListItemType.AlternatingItem _
                  Or e.Row.RowType = ListItemType.SelectedItem Then
                Dim str As String = "javascript:__doPostBack('" + gvSelectAddress.UniqueID.ToString + "','" + e.Row.RowIndex.ToString + "')"
                e.Row.Attributes.Add("onmouseover", "'Magenta';'hand'")
                e.Row.Attributes.Add("onmouseout", "'#EEEEEE';")

                e.Row.Attributes.Add("onclick", str)

            End If

    Protected Sub gvSelectAddress_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSelectAddress.SelectedIndexChanged

          gvSelectAddress.SelectedRow.Attributes.Item("onmouseover") = "'hand'"
                End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                       Dim db As Database = DatabaseFactory.CreateDatabase()

                Dim ds As DataSet = db.ExecuteDataSet(db.GetSqlStringCommand("select top 100 *  from address"))
                gvSelectAddress.DataSource = ds
        End Sub


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now