慎用DataGrid的CurrentRowIndex操作DataTable

' 在 DataGrid 的 AllowSorting 值为 True 的情形下 , 利用 CurrentRowIndex 来取 DataTable 中相应的 Row 是不大妥当的。

' 在随意中进行了某列排序后 ,CurrentRowIndex 与 DataTable 的“ CurrentRowIndex ”就有可能不相符 ,

'DataTable1.Rows[DataGrid1.CurrentRowIndex] 可能并不是你真正要的。

' 稳妥的方法可以这样:

Dim MyCurrencyManager As CurrencyManager

Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load

'......

MyCurrencyManager = Me .BindingContext(DataGrid1.DataSource, DataGrid1.DataMember)

End Sub

Private Sub Button2_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

CType (MyCurrencyManager.Current, DataRowView).Row.Delete()

End Sub

Published At
Categories with Web编程
Tagged with
comments powered by Disqus