want value of field in selected row in gridview

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hi,

I want to get the value of a particular field ("ID") of the selected row in
a gridview.
Here my code:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" />
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Preview" />
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"
ReadOnly="True"/>
<asp:BoundField DataField="titel" HeaderText="titel"
SortExpression="titel"/>
</Columns>
</asp:GridView>

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim vldna As String
vldna = GridView1.Controls.Item(1).ToString
End Sub

But i get the error:
Specified argument was out of the range of valid values.
Parameter name: index

Thanks for help
Bob
 
Hi,

You should use the SelectedRow property of the GridView to get the selected
row, then use the Cells property to select the desired cell.

Hope You find this useful.
-Zsolt
 
Hi,

I want to get the value of a particular field ("ID") of the selected row in
a gridview.
Here my code:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" />
 <Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Preview" />
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"
ReadOnly="True"/>
<asp:BoundField DataField="titel" HeaderText="titel"
SortExpression="titel"/>
</Columns>
</asp:GridView>

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim vldna As String
vldna = GridView1.Controls.Item(1).ToString
End Sub

But i get the error:
Specified argument was out of the range of valid values.
Parameter name: index

Thanks for help
Bob

Try following

GridViewRow row = GridView1.SelectedRow
Dim lblID As Label = (Label) row.FindControl("id")
vldna = lblID.Text
 
thanks both

"Alexey Smirnov" <[email protected]> schreef in bericht
Hi,

I want to get the value of a particular field ("ID") of the selected row
in
a gridview.
Here my code:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
/>
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Preview" />
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"
ReadOnly="True"/>
<asp:BoundField DataField="titel" HeaderText="titel"
SortExpression="titel"/>
</Columns>
</asp:GridView>

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal
e
As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim vldna As String
vldna = GridView1.Controls.Item(1).ToString
End Sub

But i get the error:
Specified argument was out of the range of valid values.
Parameter name: index

Thanks for help
Bob

Try following

GridViewRow row = GridView1.SelectedRow
Dim lblID As Label = (Label) row.FindControl("id")
vldna = lblID.Text
 
Back
Top