Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Full Row Selection in a Datagrid

Posted on 2006-05-11
Medium Priority
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

Expert Comment

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


Expert Comment

ID: 16657789
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", "this.style.cursor='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

ID: 16658603
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", "this.style.cursor='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)


Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 61

Author Comment

ID: 16662061

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.

Assisted Solution

SandeepRR earned 1000 total points
ID: 16665216
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", "this.style.cursor='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

Rejojohny earned 1000 total points
ID: 16666715
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

ID: 16667612

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

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

ID: 16672167
Sandeep, That code converter is a great reference!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

578 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