2

使用 Datagridview 控件和 BindingNavigator 控件的表单出现问题。最初我有以下代码:

BS = New BindingSource(DS, dsTable)  ' (earlier code) Dim BS as New BindingSource
BNV1.BindingSource = BS     ' A BindingNavigator
DGV1.DataSource = BS        ' A DataGridView

一切正常,但是当我插入一个新行时,它显示在 DataGridView 列表的末尾,我希望它显示在正确的顺序位置。

我在某个论坛的某个地方找到了一个建议,该建议建议在 Datatable 上使用 DataView 来解决这个问题。我试过了,效果很好——几乎。这是新代码:

BS = New BindingSource(DS, dsTable)
DV.Table = DS.Tables(dsTable)       ' (earlier code)  Dim DV as New DataView
DV.Sort = DS.Tables(dsTable).Columns(0).ColumnName
DGV1.DataSource = DV                 < ---- instead of the BindingSource 'BS'
BNV1.BindingSource = BS

新记录显示在正确排序的位置 - 但是 - 当我滚动 DataGridView 时,BindingNavigator 没有改变。它从未反映正确的记录号。
但是当您单击 BindingNavigator 的 moveNext、Previous、Start 和 End 箭头时,它会正确更改计数。DataGridView 中没有发生任何事情,但似乎 BindingNavigator 正在跟踪幕后的其他内容。我想那是原来的桌子。

知道如何将 BindingNavigator 同步到 DataView 吗?那么 DatagridView 和 BindingNavigator 正在查看相同的数据吗?

(我知道我可以通过每次重新填充 DataGridView 来解决这个问题,但这似乎很浪费资源。)

附加信息:实际执行插入/添加的代码

Dim NewRow As DataRow = DT.NewRow
modCommonMaint.FillNewRow(NewRow, pnlData, dicFields)
NewRow(CntText + 1) = 0
NewRow(CntText + 2) = NewRow(0).ToString
LastAdd = txtID.Text.ToUpper.Trim
DT.Rows.Add(NewRow)
DGV1.Refresh()
Try
  DA.InsertCommand = cmdA
  DA.UpdateCommand = cmdU
  DA.Update(DS, dsTable)
Catch ex As Exception
  Console.WriteLine("Error" & ex.ToString)
Finally
End Try

这是实际执行插入/添加的代码。在任何一种情况下它都不会改变。为了改变这种行为,我所做的就是交换本笔记前 2 个代码块中的行。

4

0 回答 0