Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net GridView行到对象类型_Asp.net_Vb.net - Fatal编程技术网

Asp.net GridView行到对象类型

Asp.net GridView行到对象类型,asp.net,vb.net,Asp.net,Vb.net,在ASP.NET中,如果我使用对象数组对gridview进行数据绑定,比如说,当用户选择行时,我如何检索和使用foo(索引) i、 e 在行选择 Private Sub gv1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv1.RowCommand If e.CommandName = "Select" The

在ASP.NET中,如果我使用对象数组对gridview进行数据绑定,比如说,当用户选择行时,我如何检索和使用foo(索引)

i、 e

在行选择

Private Sub gv1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv1.RowCommand

        If e.CommandName = "Select" Then
            'get and use foo(index)    
        End If
    End Sub

理论上,行的索引应该是foo的索引(对于标题行,可能+1,您需要进行测试)。所以,你应该能够按照这些思路做一些事情

dim x as object = foo(e.row.selectedIndex)

另一种方法是找到将索引数据绑定到按钮的commandArgument属性的方法

可能有一种更简洁的方法可以做到这一点,但是您可以将行的CommandArgument属性设置为其索引。然后像
foo(CInt(e.CommandArgument))
这样的东西就会起作用。

如果可以确保数据源中项目的顺序没有改变,可以使用CommandEventArgs的CommandArgument属性

但是,一种更健壮的方法是使用GridView的DataKeys/SelectedDataKey属性。唯一需要注意的是,您的命令必须是“Select”类型(因此,默认情况下,RowCommand将无权访问DataKey)

假设构成列表的实体具有某种唯一性,则可以在GridView的DataKeys属性中设置一个或多个键属性名称。设置GridView中的选定项后,可以检索键值并在绑定列表中找到该项。此方法可以解决GridView中的序号位置与数据源中元素的序号位置不匹配的问题

例如:

<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True" 
    DataKeyNames="Name" onrowcommand="GridView1_RowCommand1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
</asp:GridView>
另一种选择是在GridView的Rows集合中进行探索,通过获取控件值一次获取一列的值,但除非必须这样做,否则不建议这样做

希望这有帮助

<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True" 
    DataKeyNames="Name" onrowcommand="GridView1_RowCommand1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">
</asp:GridView>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
   // Value is the Name property of the selected row's bound object.
   string foo = GridView1.SelectedDataKey.Value as string; 
}