[Webinar] Learn how to a build a cloud-first strategyRegister Now


Selecting a row from gridview by clicking anywhere on the row

Posted on 2006-05-30
Medium Priority
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
  • 2
LVL 19

Accepted Solution

DreamMaster earned 1000 total points
ID: 16790176
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

ID: 16790919
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

ID: 16791079
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", "this.style.backgroundColor='Magenta';this.style.cursor='hand'")
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#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") = "this.style.cursor='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

Independent Software Vendors: 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!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

864 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