Reference Datagrid cell contents

G

Guest

Hi. I have an asp:ImageButton which appears when a datagrid row is placed in
Edit mode. The button has it's own click event which fires when the button
is clicked. Within the button's click event, I need to reference the
contents of a cell within the record currently in edit mode.

For example...
The datagrid has 3 columns: ID, Name, Picture
When placed in edit mode my asp:ImageButton appears in the Picture column,
and the ID and Name fields become editable. Within the sub that handles the
asp:ImageButton click event, I need to reference the contents of the Name
column.

I've tried...
Dim Name as String
Name = (CType(DataGrid1.Items.Cells(2).Controls(0), TextBox).Text)
Which does not work.

How can I accomplish this? Thanks!
 
E

Elton Wang

Hi Mr. Mike,

You can try following code.

Dim btn As ImageButton = CType(sender, ImageButton)
Dim item As DataGridItem = CType(btn.NamingContainer,
DataGridItem)
Dim Name As String = CType(item.Cells(2).Controls(0),
TextBox).Text

HTH

Elton Wang
(e-mail address removed)
 
Joined
Mar 11, 2010
Messages
1
Reaction score
0
I know this post was started in 05, but I need to know something about the solution provided here. In the example, there was an image button to reference. In my case, I have a run-time created datagrid that I want to access the cell contents in a similar way.

Dim conn as SqlConnection
Dim comm as SqlCommand
conn = New SqlConnection(cnSQLDB)
'First find the Attendee row
comm = New SqlCommand("SELECT regisID, contactID, addressIDComp, requirementsID FROM attendeeList WHERE userName=@userName")
comm.Parameters.Add("@userName",System.Data.SqlDbType.NVarChar, 30)
comm.Parameters("@userName").Value = SessionIdentifier.Text
Dim daAddress as SqlDataAdapter = New SqlDataAdapter(comm)
Dim dsAddress as DataSet = New DataSet
'Then populate dataset, datagrid with that data
daAddress.Fill(dsAddress)
Dim dgAddress as DataGrid = New DataGrid
dgAddress.DataSource = dsAddress
dgAddress.DataBind()
'Then access the values in the datagrid by placing the datagrid in edit mode
dgAddress.EditItemIndex = 0 ' or should this be a 1 for the first row?
dgAddress.DataBind()
Dim item As DataGridItem = CType(btn.NamingContainer, DataGridItem)
Dim regisID As String = CType(item.Cells(0).Controls(0), TextBox).Text
Dim contactID As String = CType(item.Cells(1).Controls(0), TextBox).Text
Dim addressIDComp As String = CType(item.Cells(2).Controls(0), TextBox).Text
Dim requirementsID As String = CType(item.Cells(3).Controls(0), TextBox).Text
'Then turn off edit mode
dgAddress.EditItemIndex = -1
dgAddress.DataBind()
'Now we can use the values from the select above in local scope variables
' complete the rest when I get the above correct...
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top