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



我正试图从DataTable填充DataGridView控件,该控件本身填充有Oracle数据库上sql查询的结果。这应该发生在双击ListBox控件中的表名之后。

第一次双击表名时效果很好,但第二次只显示列名而不显示行。

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

这是预期的行为吗?

// event handler for ListBox
private void tables_MouseDoubleClick(object sender, MouseEventArgs e)    
{
    // tables is a ListBox populated with table names
    int index = this.tables.IndexFromPoint(e.Location);     
    String table_name;
    if (index != System.Windows.Forms.ListBox.NoMatches)
    {
        // reusing the DataTable
        dt.Reset();     
        table_name =tables.Items[index].ToString();
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = con;
        // case sensitive
        cmd.CommandText = String.Format(@"select * from ""{0}""",table_name);
        cmd.CommandType = CommandType.Text;
        OracleDataAdapter da = new OracleDataAdapter(cmd);
        da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        da.Fill(dt);
        dt.TableName = table_name;
        label7.Text = table_name;
        // these three lines don't actually accomplish anything in this case
        table.DataSource = null;    
        table.Rows.Clear();
        table.DataSource = dt;
        // uncommenting these lines produces the expected behavior 
        //DataGridViewColumn c = table.Columns[0];      
        //table.Sort(c,System.ComponentModel.ListSortDirection.Descending);
    }
}

第二次双击(无排序)后

第二次双击(带排序)后

看起来问题是由DataTable.Reset调用引起的。

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

我遇到了同样的问题,但使用了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

相关内容

  • 没有找到相关文章

最新更新