datagrid in a asp.net page

J

JD

Hello Everyone,

I am writing a asp.net page using vb.net with a datagrid control. I am
trying to detect when a row is clicked on in the grid. And I am not sure how
to do this, if anyone has any ideas on this I would really appreciate your
help. Thanks.
 
B

Brock Allen

Add a button in your DataGrid, give it a CommandName="Click", then handle
the DataGrid's ItemCOmmand event. If the ItemCommandEventArgs.CommandName
matches your Button's COmmandName ("Click" in this case), then the ItemCommandEventArgs.Item
is the row that was clicked and its index in the DataGrid is ItemCommandEventArgs.Item.ItemIndex.
 
J

JD

I originally did that but the client didn't like the idea of the button on
the screen, they felt it was somewhat confusing to click on a button to
select a record and then click on another button to save the change.

Is it possible to make the datagrid work like a spreadsheet where you select
a row and highlight the selected row and then handle it appropiately.
 
E

Eliyahu Goldin

If you want server-side onclick event processing add a hidden column like
this:

<asp:ButtonColumn ButtonType="LinkButton" CommandName="Select"
Visible="False"></asp:ButtonColumn>

and handle ItemDataBound event like this:

protected void myGrid_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"] = this.GetPostBackClientHyperlink
(button, "");
}

The example presumes the the column is the very first one in the grid.


Eliyahu
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top