473,372 Members | 918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

How to determine which cell was clicked in as ASP.NET DataGrid

Hi,



I created an ASP.NET Datagrid where a single row can be selected by clicking anywhere on the row (according to http://msdn.microsoft.com/library/de...verControl.asp, Selecting Rows by Clicking Anywhere).



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

Additionally I want to be able to know which cell was clicked. Does anybody know how this can be done?



Thank you a lot in advance!



Daniel
Nov 18 '05 #1
2 3159
"Daniel Walzenbach" <da**********************@freudenberg.de> wrote in message news:e5**************@TK2MSFTNGP10.phx.gbl...
Hi,



I created an ASP.NET Datagrid where a single row can be selected by clicking anywhere on the row (according to http://msdn.microsoft.com/library/de...verControl.asp, Selecting Rows by Clicking Anywhere).



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

Additionally I want to be able to know which cell was clicked. Does anybody know how this can be done?



Thank you a lot in advance!


I'm not certain, but I think that if you use something other than "" for the second parameter to GetPostBackClientHyperlink, then the parameter will be passed as Request.Form("__EVENTARGUMENT").
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #2
Hi Daniel,

From your description, you've made the WebForm DataGrid fire the Select
Command when click anywhere on a datagrid row via the following tech
article:
http://msdn.microsoft.com/library/de...us/dv_vstechar
t/html/vbtchTopQuestionsAboutASPNETDataGridServerControl. asp

I've read the above article and also generate some tests based on it. Since
the webform DataGrid is different from winform which doesn't provide
buildin means to get the current clicked cell because the Webform datagrid
is actually rendered out to clientside as a <table> ....</table> html
element. And each row of the dataGrid is a <tr> ...</tr>
each cell is a <td>... </td>

And in the article, it make use of the ItemDataBound event and add a
"onclick" clientside event for the DataGridItem Row so that when we click
on anywhere within a DataGrid Row , it'll fire the select command event.
However, the select command doesn't contain any information about which
Cell(Column) in the DataGrid row is clicked, yes?
I've tried John's suggestion on using the second param of the
Page.GetPostBackClientHyperlink(button, "") method , but found it won't
work for this issue.

Currently, my solution on this problem is still using the ItemDataBound
event, in addition to add and "onclick" clientside event for each
DataGridItem, we also have to add a "onclick" event functoin for each Cell
of the Item, forexample,
Dim col As Integer
For col = 0 To e.Item.Cells.Count - 1
e.Item.Cells(col).Attributes.Add("onclick",
"setCurrentColumn('" & col & "')")
Next

And the "setCurrentColumn" is a javascript function in the Page which set a
value to a <input type=hidden ..> elemetn in the page. You can see we use
this javascript to assign the current clicked Cell(<td>..</td>)'s columsn
index to the <input type=hidden ..> element. And we need to put the row
information of the currrent click Cell's Row index in the LinkButton's
CommandArgument as below:
Dim button As LinkButton = _
CType(e.Item.Cells(0).Controls(0), LinkButton)

Dim row As Integer = e.Item.ItemIndex
button.CommandArgument = row.ToString()
Then, in the DataGrid's ItemCommand event, we can get the current selected
Cell's row/col info like below:
Private Sub dgMain_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
dgMain.ItemCommand
If e.CommandName = "Select" Then
Response.Write("<br>Row: " + e.CommandArgument)
Response.Write("<br>Col: " + hdCell.Value)

End If

End Sub
To make it clearly, I've pasted my test page's code at the bottom of this
message. Please refer to it if you have anything unclear. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

========================aspx ==============================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>CellGrid</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function setCurrentColumn(col)
{
document.getElementById("hdCell").value = col;
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td></td>
</tr>
<tr>
<td>
<asp:DataGrid id="dgMain" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:ButtonColumn Text="Select" HeaderText="Select"
CommandName="Select"></asp:ButtonColumn>
<asp:BoundColumn DataField="index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="email"
HeaderText="Email"></asp:BoundColumn>
</Columns>
</asp:DataGrid></td>
</tr>
<tr>
<td><INPUT id="hdCell" type="hidden" name="hdCell" runat="server"
value="0|0"></td>
</tr>
</table>
</form>
</body>
</HTML>
===============code behind======================================
Public Class CellGrid
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents dgMain As System.Web.UI.WebControls.DataGrid
Protected WithEvents hdCell As
System.Web.UI.HtmlControls.HtmlInputHidden

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Bind_Data()
End If
End Sub

Private Sub Bind_Data()
Dim tb As New DataTable("MyUser")
tb.Columns.Add("index", GetType(Integer))
tb.Columns.Add("name", GetType(String))
tb.Columns.Add("email", GetType(String))

Dim row As DataRow
Dim i As Integer
For i = 1 To 10
row = tb.NewRow()
row("index") = i
row("name") = "Name" + i.ToString()
row("email") = "Name" + i.ToString() + "@test.com"

tb.Rows.Add(row)
Next

dgMain.DataSource = tb
dgMain.DataBind()

End Sub

Private Sub dgMain_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgMain.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)

Dim row As Integer = e.Item.ItemIndex
button.CommandArgument = row.ToString()
e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(button, "")

Dim col As Integer
For col = 0 To e.Item.Cells.Count - 1
e.Item.Cells(col).Attributes.Add("onclick",
"setCurrentColumn('" & col & "')")
Next

End If

End Sub

Private Sub dgMain_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles dgMain.SelectedIndexChanged

End Sub

Private Sub dgMain_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
dgMain.ItemCommand
If e.CommandName = "Select" Then
Response.Write("<br>Row: " + e.CommandArgument)
Response.Write("<br>Col: " + hdCell.Value)
'Response.Write("<br>CommandArgument: " + e.CommandArgument)
'Response.Write("<br>CommandSource: " +
e.CommandSource.ToString())
End If

End Sub
End Class

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Charlie | last post by:
Hi: Using the WinForms DataGrid, I would like to have the entire row highlight rather than just the cell that was last clicked. How do you do this? Thanks, Charlie
1
by: orekinbck | last post by:
Hi There I have a datagrid whose main purpose in life is to provide a nice way for users to make a single choice from a list. The grid is read only, single row select and has its data source as...
6
by: EMW | last post by:
How can I detect on which cell the user has clicked on a datagrid webcontrol? rg, Eric
1
by: Tor Inge Rislaa | last post by:
Find what cell is clicked in a datagrid When clicking a cell in the datagrid I can't see that the click event is raised. As I can understand the click event is raised when the column or row...
21
by: coleenholley | last post by:
I've been trying since last Friday to get an answer on how to get a SPECIFIC row.cell value from a datagrid. I've had plenty of suggestions, but nothing works to get the value from a SPECIFIC Row...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
0
by: theintrepidfox | last post by:
Dear Group I really need your help on this issue. I've a datagrid which source is a dataview based on a dataset. The columns of the grid are datagridtextbox columns created like the following...
2
by: benfly08 | last post by:
Hi, everyone. I got a weird problem for my C# program. In my program, I use a DataGrid to display data. I bind the DataGrid to a DataTable. I made all the cells in DataGrid ReadOnly as True. So...
0
by: =?Utf-8?B?TWljaGFlbCBKLiBWaXNpbnRpbg==?= | last post by:
Hi All, I am implementing the Paint Method override described in the Help (DataGridTextBoxColumn.PaintMethod). I believe that I have implemented the procedure example displayed in the Help...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.