Select gridwiev on mouse click in asp.net

Is it possible to select gridview row on mouse click on row (like in winforms), idea is that user can click anywhere on row and to select clicked row (of course color of selected row must change on selection)
LVL 1
kahvedzicAsked:
Who is Participating?
 
sujeshvaCommented:
do you want to do it on the client side? I had written an entire article to do this. It uses JQuery. Maybe it might help you...

go to http://sujesharukil.blogspot.com and look at the blog post about Gridview.

-Suj
0
 
kahvedzicAuthor Commented:
As much as I can understand from your post this is code for changing row colors in gridview. I first need to be able to select gridview row without select button, just click anywhere on row.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Akin DeluI’m a software engineer, tech enthusiast and UI/UX lover. Commented:
this is a sample code
=====================
in your server side code

VB
=====
Protected Sub GridView1_RowCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("onclick", "if(this.style.backgroundColor=='rgb(193, 210, 238)'){this.style.backgroundColor='rgb(255, 255, 255)';}else{this.style.backgroundColor='rgb(193, 210, 238)';}")
        End If
    End Sub

C#
===
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "if(this.style.backgroundColor=='rgb(193, 210, 238)'){this.style.backgroundColor='rgb(255, 255, 255)';}else{this.style.backgroundColor='rgb(193, 210, 238)';}");
            }
        }
0
 
rajapandian_81Commented:
Use RowDataBound of grid.

protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
      e.Row.Attributes.Add("onclick", "this.style.color = 'red';");
}

Open in new window

0
 
kahvedzicAuthor Commented:
This is what I did:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';")
            e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString))
        End If
    End Sub

and in html code:
created new style
    <style type="text/css">    
        .HiddenColumn{display:none;}
    </style>

for selected button that was in gridview:
<asp:CommandField ButtonType="Image" ShowSelectButton="True" ItemStyle-CssClass ="HiddenColumn" HeaderStyle-CssClass ="HiddenColumn" SelectImageUrl="~/Images/check.gif" >

This is working, but I have on GridView1_SelectedIndexChanged event

GridView1.SelectedRow.Focus()

but it is not working, my GridView is in asp Panel with vertical scroll bar, and when I select  row that I must scroll to see focus is not working.
0
 
kahvedzicAuthor Commented:
ok I will take a look, thanks
0
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.

All Courses

From novice to tech pro — start learning today.