want value of field in selected row in gridview


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
 
Ad

Advertisements

M

miher

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
 
G

Guest

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
 
Ad

Advertisements

B

Bob

thanks both

"Anon User" <> 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
 

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

Top