请问vb.net 如何得到datagrid中选取列在datatable中的行号

zsylizard 2005-08-03 10:11:49
当将datagrid 梆定到datatable上后,当选取datagrid某一行时,如何知道选取行在datatable中的行号.
...全文
348 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
filebat 2005-08-14
  • 打赏
  • 举报
回复
'我写了一种方法,通过遍历来做的.
'可以实现效果,不过效率不高。
'关键是不知道能datarow如何得到datatable的rowindex.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'控件DataGrid1的DataSource为dtable(dtable为一个datatable)
'返回排序后datatable对应的记录的行号
Dim result As Integer = -1 '查找到的结果,如果失败则,返回-1
Dim index As Integer '记录着DataGrid1中的CurrentRowIndex()
Dim str As String '用于调试的输出信息
Dim row1, row2 As DataRow 'row1为DataGrid1的CurrentRow,而row2用来遍历dtable
Dim i As Integer
i = dtable.Rows.Count - 1 '从后向前扫描
index = Me.DataGrid1.CurrentRowIndex()
row1 = dtable.Rows(index)
While i > -1
row2 = dtable.DefaultView.Item(i).Row
If row1 Is row2 Then
result = i
Exit While
End If
str = "dtable.row(" & index & "):" & vbCr
str = str & row1.Item(0) & "," & row1.Item(1) & vbCr
str = str & "dtable.DefaultView.Item(" & i & ").Row:" & vbCr
str = str & row2.Item(0) & "," & row2.Item(1) & vbCr
MessageBox.Show(str)
i = i - 1
End While
MessageBox.Show("Datagrid1当前记录对应于dtable的第" & (result + 1) & "行")
End Sub
ccghghgj 2005-08-14
  • 打赏
  • 举报
回复


===============================
CSDN小助手 是一款脱离浏览器也可以使用csdn论坛的
软件!
界面: http://qqwwee.com/
下载: http://qqwwee.com/csdn.rar 包含源代码
zsylizard 2005-08-13
  • 打赏
  • 举报
回复
没有人知道吗?排序后怎么处理
zsylizard 2005-08-05
  • 打赏
  • 举报
回复
如rainmanguo() 所说的,如果对datagrid进行排序后,此时得到的datagrid行号和datatable里的行号就不同了.
rainmanguo 2005-08-03
  • 打赏
  • 举报
回复
添加事件:DataGrid1_Click(...),代码如下:

private Ds as New DataSet
Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
Dim rowNum As Integer
Dim AllRow() As DataRow
Dim i, Locate, rowTotle As Integer

AllRow = Ds.Tables("datatable").Select()
rowTotle = Ds.Tables("datatable").Rows.Count
For i = 0 To rowTotle - 1
If Me.DataGrid1.Item(rowNum, 0) = AllRow(i).Item(0) Then
Locate = i
Exit For
Else
Locate = -1
End If
Next
End Sub
wolf_fq 2005-08-03
  • 打赏
  • 举报
回复
是這樣的:
myDataTable.defaultView.item(myDataGrid.currentRowIndex)
rainmanguo 2005-08-03
  • 打赏
  • 举报
回复
有时经过过滤,datagrid中不是表中的全部记录,这时他们的行号不一样。
rainmanguo 2005-08-03
  • 打赏
  • 举报
回复
漏了一句:
rowNum = Me.DataGrid1.CurrentCell.RowNumber
我也觉得DataGrid经过绑定,它的行排序和表里的行排序应该一样。
zzmdegm 2005-08-03
  • 打赏
  • 举报
回复
Me.datagrid.CurrentRowIndex = datatable.Rows.Count - 1

datatable.Rows.Count - 1 =Me.datagrid.CurrentRowIndex
zzmdegm 2005-08-03
  • 打赏
  • 举报
回复
datagrid不是有当前行的行号吗,只要是邦定的那个表,这个行号就是表的行号啊

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧