WinForms C# - Display DataGridView

本文关键字:DataGridView Display WinForms | 更新日期: 2023-09-27 18:06:42

我开发了一个WinForms应用程序,我想通过DataGridView显示数据。但是,DataGridView不显示

我决定使用Visible属性而不是使用多个窗体(当我发现"Tabless TabControl"技巧时已经太晚了,因为我已经在面板中创建了控件)

场景如下:

默认情况下,我的所有控件都是Visible (=true)。当我启动应用程序时,显示主面板。然后点击搜索按钮,主面板通过设置其Visible属性为false隐藏,搜索面板通过设置其Visible属性为true显示。

这是功能齐全的:我的搜索面板显示(由于不同的背景颜色测试),但我的DataGridView里面没有显示。

代码如下:

private void iSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    if (_mySqlCeEngine.DatabaseExists())
    {
        dgv_customer.DataSource = _mySqlCeEngine.GetCustomers();
        ShowSearchCustomerPanel();
    }
}

如果本地数据库文件存在,我给DataTable充满数据的DataGridView通过GetCustomers()方法,然后显示我的搜索面板:

private void ShowSearchCustomerPanel()
{
        pnl_home.Visible = false;
        pnl_searchCustomer.Visible = true;
}

我拼命地尝试使用Application.DoEvents()和/或dgv_customer.Refresh()或再次重新创建DataGridView,但没有什么是正确的。

谢谢,如果有英文错误,我很抱歉。

悍妇。

WinForms C# - Display DataGridView

如果更改Visible属性值,DataGridView类中会出现一个错误。

解决这个bug的方法是:

  • DataGridView从其父Controls.Remove()中移除以隐藏它;
  • 将其读取到父Controls以再次显示。