更改DataSource后,为什么DataGridView仅在排序后显示行?

时间:2016-05-08 09:51:00

标签: c# winforms datagridview oracle12c

我正在尝试从DataTable中填充DataGridView控件,该数据集本身充满了Oracle数据库上的sql查询结果。双击ListBox控件中的表名后,应该会发生这种情况。

这在我第一次双击表名时工作正常,但第二次它只显示列名而没有行。

我发现基于任何列对DataGridView进行排序会导致行突然出现,这让我怀疑某种图形/渲染故障。

这是预期的行为吗?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Do stuff.
End Sub

After second double-click(without sorting)

After second double-click(with sorting)

2 个答案:

答案 0 :(得分:1)

看起来问题是由DataTable.Reset电话引起的。

虽然这不是您的代码问题(很可能是DataTableDataView中的错误),但我建议您使用Clear方法代替Reset或创建新{ {1}}实例。

答案 1 :(得分:0)

我有同样的问题,但是使用VBA。附件是我的解决方案。

物品说明... CSV_IO_INSTANCE是一个其中包含数据表DATA_TABLE的类。我需要将新的csv数据重新加载到DATA_TABLE的此实例中,然后刷新。

代码...

    Private Sub SHARED_CREATE_DATA_TABLE()
    Try
        DATA_GRID_VIEW_CSV_IO.DataSource = CSV_IO_INSTANCE.DATA_TABLE
        '^BIND DGV TO LOCAL INSTANCE OF THE CSV_IO TABLE.
        '--------------------------------------------------------------------------------------
        'BUG FIX 011 - SEE BUG_FIX_LOG.txt FOR DOCUMENTATION.
        Dim COLUMN_BUG_FIX As DataGridViewColumn = DATA_GRID_VIEW_CSV_IO.Columns(0)
        '^CREATE A COLUMN TO DO BUG FIX ON.
        COLUMN_BUG_FIX.SortMode = DataGridViewColumnSortMode.NotSortable
        '^CHANGE ITS SORT MODE.
        DATA_GRID_VIEW_CSV_IO.Sort(COLUMN_BUG_FIX, System.ComponentModel.ListSortDirection.Ascending)
        '^SORT.
        DATA_GRID_VIEW_CSV_IO.Sort(COLUMN_BUG_FIX, System.ComponentModel.ListSortDirection.Descending)
        '^JUST ONE SORT WONT DO IT.
        COLUMN_BUG_FIX.SortMode = DataGridViewColumnSortMode.Automatic
        '^CHANGE BACK TO AUTO SORT TO GET RID OF THAT SORT ARROW IN THE COLUMN HEADER.
        '--------------------------------------------------------------------------------------
    Catch EXCEPTION As Exception
        CSV_IO_INSTANCE.LOG.WRITE_LINE("Exception in the SHARED_CREATE_DATA_TABLE subroutine.", EXCEPTION)
        '^WRITE ERROR MESSAGE TO THE EVENT_LOG INSTANCE (LOG) IN THE CSV_IO INSTANCE.
    End Try
End Sub

编辑:

我发现此论坛帖子中的一些帖子有助于将我引向正确的方向: https://social.msdn.microsoft.com/Forums/windows/en-US/16398f0c-9687-42aa-8678-e742a43c00c5/reset-the-sort-status-after-reloading-a-datagridview?forum=winformsdatacontrols

编辑X2:

好的,第一个解决方案没有完全起作用。当它删除列标题上的排序图标时,它使该列保持排序状态。我通过此链接在一些帮助下对其进行了重新设计: https://social.msdn.microsoft.com/Forums/vstudio/en-US/1a9350da-3575-4f79-b708-4277e4d6a240/how-to-force-datagridview-back-to-an-unsorted-state?forum=vbgeneral

现在我有了一个有效的模型,由于其RemoveSort功能,我正在使用绑定源。我先调用FLUSH_DGV_AND_BS,然后重新填充数据表,然后再调用BIND_DGV_TO_BS,这很好用。

Private Sub BIND_DGV_TO_BS()
    BINDING_SOURCE.DataSource = CSV_IO_INSTANCE.DATA_TABLE
    '^BIND DATASET TO THE INSTANCES DATA TABLE
    DATA_GRID_VIEW_CSV_IO.DataSource = BINDING_SOURCE
    'BUG FIX 011 - SEE BUG_FIX_LOG.txt FOR DOCUMENTATION.--------------------------------------
    Dim COLUMN_BUG_FIX As DataGridViewColumn = DATA_GRID_VIEW_CSV_IO.Columns(0)
    '^CREATE A COLUMN TO DO BUG FIX ON.
    DATA_GRID_VIEW_CSV_IO.Sort(COLUMN_BUG_FIX, System.ComponentModel.ListSortDirection.Ascending)
    '^SORT IN ORDER TO CAUSE THE BUG TO GO AWAY.
    BINDING_SOURCE.RemoveSort()
    '^THEN REMOVE THE SORT TO PUT IT BACK TO ITS RAW DATA VIEW.
    '------------------------------------------------------------------------------------------
End Sub
Private Sub FLUSH_DGV_AND_BS()
    With BINDING_SOURCE
        .DataSource = Nothing
        '^REMOVE THE PREVIOUS BINDINGS.
    End With
    CSV_IO_INSTANCE.DATA_TABLE.Clear()
    '^CLEAR OUT THE DATA TABLE.
End Sub