【问题标题】:Change visible state of DataGridView only given its name仅根据名称更改 DataGridView 的可见状态
【发布时间】:2019-05-10 22:57:34
【问题描述】:

我有一个表单应用程序,其中要显示多个 DataGridView 对象(但不是一次)。它们应该在彼此之上创建,然后应该可以使用 ComboBox 切换显示的 DataGridView。 我有一个函数,它应该在每次调用它时创建新的 DataGridView,然后将名称添加到 ComboBox:

private void readCSV(string DBname)
{
    DataGridView tagDBname = new DataGridView();
    tagDBname.Location = new System.Drawing.Point(24, 260);
    tagDBname.Name = DBname;
    tagDBname.Size = new System.Drawing.Size(551, 217);
    tagDBname.TabIndex = 6;

    tagDBname.Columns.Add("Column1", "Col1");
    tagDBname.Columns.Add("Column2", "Col2");

    tagDBname.Visible = false;

    comboBoxTag.Items.Add(DBname);
}

然后,我想根据 ComboBox 中的选定名称更改 DataGridView 的可见性状态。这应该在索引更改时调用的函数中完成:

private void comboBoxTag_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the name of the DataGridView which should be visible:
    string selectedTagDB = comboBoxTagDatabases.SelectedItem.ToString();
    DataGridView tagDatabase = ? // Here the DataGridView should be selected given the name "selectedTagDB"

    tagDatabase.Visible = true;
}

在上面,我不知道如何分配DataGridView,只给出了它的名字。任何帮助将不胜感激 - 即使这意味着所选方法不适合我想要实现的目标。如果问题在其他地方得到解答,请随时指导我正确的方向:)

【问题讨论】:

  • 可能重复:Find control by name
  • 您想显示名称为 N 的 DGV 并隐藏所有其他的?
  • @Cid 是的,完全正确

标签: c# winforms datagridview


【解决方案1】:

我会使用数据库名称作为键将网格视图存储在字典中;

private readonly Dictionary<string, DataGridView> _tagDBs =
    new Dictionary<string, DataGridView>();

private void readCSV(string DBname)
{
    DataGridView tagDBname = new DataGridView();

    // Add the gridview to the dictionary.
    _tagDBs.Add(DBname, tagDBname);

    tagDBname.Name = DBname;
    tagDBname.Location = new System.Drawing.Point(24, 260);
    tagDBname.Size = new System.Drawing.Size(551, 217);
    tagDBname.TabIndex = 6;

    tagDBname.Columns.Add("Column1", "Col1");
    tagDBname.Columns.Add("Column2", "Col2");

    tagDBname.Visible = false;

    this.Controls.Add(tagDBname); // Add the gridview to the form ot to a control.
    comboBoxTag.Items.Add(DBname);
}

private void comboBoxTag_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the name of the DataGridView which should be visible:
    string selectedTagDB = comboBoxTagDatabases.SelectedItem.ToString();

    foreach (DataGridView dgv in _tagDBs.Values) {
        dgv.Visible = dgv.Name == selectedTagDB; // Hide all gridviews except the selected one.
    }
}

如果你需要对选中的gridview做点什么,你可以通过:

if (_tagDBs.TryGetValue(selectedTagDB, out DataGridView tagDatabase)) {
    // do something with tagDatabase.
}

注意:您必须将 gridview 添加到表单或表单上的容器控件中。例如

this.Controls.Add(tagDBname);

【讨论】:

  • 感谢您的回答。您知道我如何确定 DGV 应该显示在哪个选项卡上吗?表单有 TabControl 布局,而 DGV 没有出现 this.Controls.Add(tagDBname)。
  • 此要求与您的代码不匹配。如果网格视图出现在不同的标签页上,您可以立即使它们全部可见,并且不需要组合框进行选择,因为用户将选择标签页。这使选定的网格视图自动可见。见Dynamically add tabs to TabControl container
  • 所有创建的 DGV 都应该在一个单独的选项卡上,而不是不同的选项卡。我是否错过了在初始化 DGV 时应在哪个选项卡上显示 DGV 的参数?因为它们没有出现在表单上。
  • 标签页有名字。例如:tabPage1.Controls.Add(tagDBname);.
  • 非常感谢,只是我缺少的细节。现在一切正常! :)
【解决方案2】:

您可以遍历表单的所有DataGridViews 以使用其名称显示预期的,同时隐藏其他的。

这个解决方案并不漂亮,但很有效

private void ShowOneDataGridViewAndHideOthers(string name)
{
    foreach (var DGV in this.Controls.OfType<DataGridView>())
    {
        DGV.Visible = DGV.Name == name;
    }
}

这样称呼它:

private void comboBoxTag_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the name of the DataGridView which should be visible:
    string selectedTagDB = comboBoxTagDatabases.SelectedItem.ToString();
    ShowOneDataGridViewAndHideOthers(selectedTagDB);
}

这种方法可以更通用一点:

private void ShowOneControlAndHideOthers<T>(string name, Control controls) where T : Control
{
    foreach (var control in controls.Controls.OfType<T>())
    {
        control.Visible = control.Name == name;
    }
}

private void comboBoxTag_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the name of the DataGridView which should be visible:
    string selectedTagDB = comboBoxTagDatabases.SelectedItem.ToString();
    ShowOneControlAndHideOthers<DataGridView>(selectedTagDB, this);
}

【讨论】:

  • 相当不错的解决方案,请注意 this.Controls 需要成为 DataGridViews 的父控件。使用下面的字典方法不需要知道父母
猜你喜欢
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 2017-11-27
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多