仅给定名称,更改DataGridView的可见状态

时间:2019-05-10 13:48:28

标签: c# winforms datagridview

我有一个表单应用程序,其中要显示多个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);
}

然后,我想更改CGridBox中给定名称的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。任何帮助将不胜感激-即使这意味着所选择的方法与我要达到的目标不匹配。如果在其他地方回答了问题,请随时向正确的方向引导我:)

2 个答案:

答案 0 :(得分:4)

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

readonly

如果您需要对选定的gridview做一些事情,可以使用以下方法获得它:

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.
}

答案 1 :(得分:2)

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

这种解决方案虽然不完美,但可以解决

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);
}