Full Row Selection in a Datagrid

Posted on 2006-05-11
Last Modified: 2012-08-13

I'd like to set up full row selection on a datagrid, so that the user can make a selection by clicking anywhere in a given row, so that I can hide or eliminate the "select" column.  Any Ideas on how to do this?

(VB.Net 2003)

Thanks for all your help.
Question by:mbizup
    LVL 3

    Expert Comment

    DataGrid1.Select(DataGrid1.CurrentRowIndex) (in the keyup event of the grid)

    LVL 6

    Expert Comment

    HI mbizup,
                 The solution to ur problem is that u have to call the method which datagrid calls when select button is clicked.

    What u have to do is that

    Add one select button to datagrid and that u can hide as per ur requirment.

    now Add following code to ur Datagridname_ItemCreated method

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
                                                    e.Item.Attributes.Add("onmouseover", "'hand'");
                                                    string jScript = "javascript:__doPostBack('" + this.UniqueID.ToString() +"$dgSearchResult$" + "_ctl" + (e.Item.ItemIndex + 3) + "$LinkButton1','')";
                            e.Item.Attributes.Add("onclick", jScript);

    where dgSearchResult is name of my datagrid

    LVL 61

    Author Comment

    Thanks for your replies.  

    The Keyup event did not show up in the VB editor's intellisense -- VB.Net 2003

    My C skills are a little rusty.  Do you know how to translate that to BASIC? This is what I have so far:

    Dim jScript as string
    if e.Item.ItemType =ListItemType.Item OR e.Item.ItemType = ListItemType.AlternatingItem OR  e.Item.ItemType = ListItemType.SelectedItem Then
          e.Item.Attributes.Add("onmouseover", "'hand'")

          '**** This is where I'm having trouble, and my interpretation...
          ' C
          'string jScript = "javascript:__doPostBack('" + this.UniqueID.ToString() +"$dgSearchResult$" + "_ctl" + (e.Item.ItemIndex + 3) + "$LinkButton1','')";
           jScript = "javascript:__doPostBack('" & this.UniqueID.ToString() & "dgSurveyResponses" & "_ctl" & (e.Item.ItemIndex + 3) & "LinkButton1,'')")

          e.Item.Attributes.Add("onclick", jScript);
    End IF

    I have a couple of questions about the jScript string:
    It doesn't compile because "This." is not recognized.  Should This.UniqueID be inside the jscript string?  
    What is LinkButton1?  Is that the name of your Select button column?
    Is e.Item.ItemIndex + 3  the location of the Select column?

    Is the resultant string supposed to look somehing like this (my datagrid is dgSurveyResponses)?

    If not, what would a harrdcoded version of this string look like?  (That might help me figure out the VB)


    LVL 61

    Author Comment


    I was unable to get this working.  I worked out the syntax, but it didn't look like the SelectedIndexChanged event was firing.  The screen would flash in response to clicking on columns other than the Select column, but the code behind the SelectedIndexChanged event did not execute.  

    I found a sloppy workaround that did the trick for this particular page.  I changed all the columns to Select columns, using the DataTextField property to associate the columns with the appropriate fields in the underlying query.

    I will ask CS to close this question.

    Thanks fpr your help.
    LVL 6

    Assisted Solution

    Hi mbizup
    First of all sorry for writing the hardcoded code and not explaining the all of my control .

    And now the solution for all of ur doubt

    The same cod ein the VB

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.SelectedItem Then
     e.Item.Attributes.Add("onmouseover", "'hand'")
     Dim jScript As String = "javascript:__doPostBack('" + Me.UniqueID.ToString + "$dgSearchResult$" + "_ctl" + (e.Item.ItemIndex + 3) + "$LinkButton1','')"
     e.Item.Attributes.Add("onclick", jScript)
    End If

    and the LinkButton1 is my button name, that u guessed right, now if u dont know the link button name then one thing u can do is run the application with select button visible and just go over to select button and see the script in the status bar of Internet Explorer.(this is what we have to call on the row click event and thi string goes in string jscript)

    And about the (e.Item.ItemIndex + 3) this u might haev to change to (e.Item.ItemIndex + 2) as per ur requirment.

    And one more thing for ur future reference, u can go this link and paste ur C# code to convert it to VB

    LVL 26

    Accepted Solution

    have a look here for an example on how to do it

    Select Row by Clicking Any Column
    Look at the following section

    The Works and Days of Hands
    Dear Web Team:
    I love how the ASP.NET DataGrid Web Control is so easy to use, but I'd like to allow the user to select a row by clicking any column, rather than having a specific selection column as shown in many samples.

    LVL 61

    Author Comment


    Thank you both for the responses.  I really would like to get this working by the methods you have posted rather than my work around.  The code makes sense to me... basically associating the click of the other columns with the select column.  


    The Me.UniqueID caused an Invalid Object error (or something like that).  When I took that part of the statement out, It compiled okay.  When I clicked on a non-select column, the screen flickered (so I feel that this is very close) but the SelectedIndexChanged code did not run.

    Can you explain what the e.Item.ItemIndex + 3 part of the code does?


    I'm pretty new to HTML... I placed the code from the URL you posted at the end of the HTML, and it just showed up as text on my page in the browser.  Roughly where in the HTML code should I place it?  I know thats a very vague question since you can't see my application, but If you give me a ballpark idea I can work with it...

    Some more info about my datagrid (I don't know if this plays a role here)... The grid is bound to fields in an SQL Server table.

    Thank you both for all the help.  
    LVL 61

    Author Comment

    Okay, both of these solutions work beautifully.  Turns out I was missing the <Script>  things in my HTML and that was why the HTML code was showing up as text in the browser (I am a noob).  Thank you both again for your help.
    LVL 61

    Author Comment

    Sandeep, That code converter is a great reference!

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    732 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

    22 Experts available now in Live!

    Get 1:1 Help Now