Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Adding click event to rows in a DataGrid

0.00/5 (No votes)
16 Jul 2006 1  
A simple way to select a DataGrid row by clicking it.

Introduction

I spent some time trying to find a way to simulate the fullRowSelect on a dataGrid, and couldn't really see any obvious way of doing it (all the solutions I tried were pretty complicated, and crashed half of the time), but after a while it hit me that you could use javascript. All you need to do is:

protected void nameOfGridView View_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'");
            e.Row.Attributes.Add("onclick", "javascript:__doPostBack" +
               "('nameOfGridView '"
               + ", 'Select$" + e.Row.RowIndex + "')");
        }
    }

Just add this method to the RowDataBound Delegate for your datagrid, and replace nameOfGridView with the name of your gridview. Thats all, easy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here