gridview 行的选定
我的gridview的数据绑定是一个datatable,这样可以根据输入的行数,动态增加表行
apsx:
Dim dt As New Data.DataTable
Dim getnumtext As Integer
Dim i As Integer
Dim s As String
dt.Columns.Add(New Data.DataColumn( "Downid ", GetType(String)))
dt.Columns.Add(New Data.DataColumn( "Downurl ", GetType(String)))
dt.Columns.Add(New Data.DataColumn( "Downfile ", GetType(String)))
getnumtext = Val(Trim(TextBox1.Text))
If getnumtext <> 0 Then
For i = 1 To getnumtext
Dim row As Data.DataRow
row = dt.NewRow()
row( "Downid ") = " "
row( "Downurl ") = " "
row( "Downfile ") = " "
dt.Rows.Add(row)
Next
GridView1.DataSource = dt.DefaultView
GridView1.DataBind()
end if
但girdview1中显示的各行编辑中,如何获得所选择的行索行值?我用以下的只能显示所选择的行各列值:
Sub _ADD_File(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
'Dim _Grcount As Integer = GridView1.Rows.Count.ToString.Trim 'get gridview1 's count
Dim i As Integer = 0
For i = 0 To _Grcount - 1
Dim _turl As TextBox = GridView1.Rows(i).FindControl( "txturl ")
Dim _tfile As TextBox = GridView1.Rows(i).FindControl( "txtfile ")
Response.Write(GridView1.Rows.Item(i).Cells(0).Text & ", " & _turl.Text & ", " & _tfile.Text & "| ")
Next
End Sub
在.net 2003上用以下命令就可以实现:
Sub _addfile(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'--1.先获取当前的行值
Dim getrowkey As Integer = e.Item.ItemIndex
end sub
而在2005中,上面的命令就不能用了。
[解决办法]
选择rowcommand事件,然后在事件处理代码中用e.Item.ItemIndex一样可以获取当前行.
[解决办法]
不懂 GridView为什么有 Item [VB.NET + C#.net 经典打造 《^.^》]
GridView1.DataSource = dt.DefaultView ;
GridView1.DataKeyField= "Downid ";
GridView1.DataBind();
<asp:Button Id= "Button1 ";...;CommandName= "Select " CommandArgument= ' <%#Eval( "Downid ")%> ' />
Sub _addfile(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
if(e.CommandName== "Select ")
{ string id=e.CommandArgument.toString();//主键值
}
end sub