Selecting row in datagrid by clicking everyway on the item

L

Liuc

I would select rows in datagrid by clicking on the desired row.
Someone csn tell me how i can do that?
Thanks.
 
M

Mike Moore [MSFT]

Hi,

This is addressed in this article:

Top Questions about the DataGrid Web Server Control
http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchTopQuestionsA
boutASPNETDataGridServerControl.asp

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 
M

Mike Moore [MSFT]

Hi,

I copied the link from my previous post, removed the carriage return, and
pasted the result into a browser and it did get the article.

In any case, here is the text from the click anywhere section of the
article.

Selecting Rows by Clicking Anywhere
The default model for selecting rows in the grid is for you to add a Select
button (actually, a LinkButton control) whose CommandName property is set
to "Select." When the button is clicked, the DataGrid control receives the
Select command and automatically displays the row in selected mode.

Not everyone likes having an explicit Select button, and a common question
is how to implement the feature where users can click anywhere in a grid
row to select it. The solution is to perform a kind of sleight-of-hand in
the grid. You add the Select LinkButton control as normal. Users can still
use it, or you can hide it. In either event, you then inject some client
script into the page that effectively duplicates the functionality of the
Select button for the row as a whole.

The example below shows how. In the grid's ItemDataBound handler, first
make sure that you are not in the header, footer, or pager. Then get a
reference to the Select button, which in this instance is assumed to be the
first control in the first cell. You then call a little-known method called
GetPostBackClientHyperlink. This method returns the name of the postback
call for the designated control. In other words, if you pass in a reference
to a LinkButton control, it returns the name of the client function call
that will perform the postback.

Finally, you assign the client-side method to the item itself. When the
grid renders, it renders as an HTML table. By assigning the method to the
item, it is the equivalent of adding client-side code to each row (<TR>
element) in the table. The grid's Item object does not directly support a
way to assign client code to it, but you can do that by using its
Attributes collection, which passes anything you assign to it through to
the browser.

Note One small disadvantage of this technique is that it adds somewhat to
the stream rendered to the browser, and it adds information for each row to
view state.
' Visual Basic
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
Dim itemType As ListItemType = e.Item.ItemType
If ((itemType = ListItemType.Pager) Or _
(itemType = ListItemType.Header) Or _
(itemType = ListItemType.Footer)) Then
Return
Else
Dim button As LinkButton = _
CType(e.Item.Cells(0).Controls(0), LinkButton)
e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(button, "")
End If
End Sub

// C#
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;
if ((itemType == ListItemType.Pager) ||
(itemType == ListItemType.Header) ||
(itemType == ListItemType.Footer))
{
return;
}
LinkButton button = (LinkButton)e.Item.Cells[0].Controls[0];
e.Item.Attributes["onclick"] =
Page.GetPostBackClientHyperlink(button, "");
}


Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top