I am using .net 2005.
I have created a GridView with one column having a command button.
The DataAdapter, DataSource, DataSet were created in code, not with wizards.
When the command button is clicked, I can't figure out the syntax for retreiving the value from the first cell in the selected row.
Can someone help?

thanks,
earlofroberts

Dani AI

Generated

asked for a web-forms solution (VS 2005), so Windows Forms snippets like gave are not applicable and ’s DataGrid sample is for a different control. ’s idea of indexing into the row/cell does work, but it is brittle (column order changes, TemplateFields, paging, or HTML in a cell break simple text access). A more robust pattern is to bind the row key with DataKeyNames and handle the GridView command event so the button sends the row index; this avoids relying on cell positions and works well with paging.

Example pattern (markup sets the data key and sends the row index in CommandArgument):

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" AutoGenerateColumns="False">
  <Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" />
    <asp:BoundField DataField="Name" HeaderText="Name" />
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Button ID="btnPick" runat="server" CommandName="Pick" CommandArgument='<%# Container.DataItemIndex %>' Text="Pick" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

Code-behind: handle RowCommand and read the DataKeys entry for that row (safe when using DataKeyNames). Also use FindControl when the value is rendered inside a control in a TemplateField.

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    If e.CommandName = "Pick" Then
        Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
        Dim keyValue As Object = GridView1.DataKeys(rowIndex).Value
        ' If using a control inside a TemplateField:
        ' Dim row As GridViewRow = GridView1.Rows(rowIndex)
        ' Dim lbl As Label = CType(row.FindControl("lblID"), Label)
        ' Dim textValue As String = lbl.Text
    End If
End Sub

Notes: ensure DataKeyNames is set before DataBind; CommandArgument is page-relative (DataKeys match the current page); if the value is a control inside a TemplateField use FindControl rather than cell text. For official details see Microsoft docs on the GridView.RowCommand Event and the GridView.DataKeys property.

Recommended Answers

All 7 Replies

This should work with a Windows Desktop App; strText = Me.dgvGrid.SelectedCells.Item(0).Value.ToString & ""

select the 1st cell of rows

dim i as integer
i = datagrid.CurrentRowIndex()

text = Trim(datagrid.Item(i, 0))

emurf,
thanks, but it's a web app.

jx_man,
thanks, but it's a gridview not a datagrid, so it doesn't have the item property.

dim string1 as string

string1 = gridview1.rows(row#).cells(cell#).tostring

now, here is the kicker. the first row is row "0"
the first column/cell is "0"

it goes against intuition of using 1...

if you still need help estenson1 at gmail

best,
eric

add after .cells(cell#).text.tostring

dim string1 as string

string1 = gridview1.rows(row#).cells(cell#).tostring

now, here is the kicker. the first row is row "0"
the first column/cell is "0"

it goes against intuition of using 1...

if you still need help estenson1 at gmail

best,
eric

**********************************************
Thanks a lot , it worked in my case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.