Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改DataGridView的可见状态(仅指定其名称)_C#_Winforms_Datagridview - Fatal编程技术网

C# 更改DataGridView的可见状态(仅指定其名称)

C# 更改DataGridView的可见状态(仅指定其名称),c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个表单应用程序,其中要显示多个DataGridView对象(但不是一次显示)。它们应该在彼此之上创建,然后应该可以使用组合框切换显示的DataGridView。 我有一个函数,它应该在每次调用DataGridView时创建新的DataGridView,然后将名称添加到组合框中: private void readCSV(字符串DBname) { DataGridView tagDBname=新DataGridView(); tagDBname.Location=新系统.图纸.点(24260

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

private void readCSV(字符串DBname)
{
DataGridView tagDBname=新DataGridView();
tagDBname.Location=新系统.图纸.点(24260);
tagDBname.Name=DBname;
tagDBname.Size=新系统.Drawing.Size(551217);
tagDBname.TabIndex=6;
tagDBname.Columns.Add(“Column1”、“Col1”);
tagDBname.Columns.Add(“Column2”、“Col2”);
tagDBname.Visible=false;
comboBoxTag.Items.Add(DBname);
}
然后,我想更改DataGridView的可见性状态,给定从组合框中选择的名称。这应该在索引更改时调用的函数中完成:

private void comboxTag\u SelectedIndexChanged(对象发送方,事件参数e)
{
//获取应可见的DataGridView的名称:
string selectedTagDB=comboBoxTagDatabases.SelectedItem.ToString();
DataGridView tagDatabase=?//此处应选择名为“selectedTagDB”的DataGridView
tagDatabase.Visible=true;
}

在上面的例子中,我不知道如何只指定DataGridView的名称。任何帮助都将不胜感激,即使这意味着所选择的方法与我试图实现的目标不符。如果这个问题在别处得到了回答,请随意为我指明正确的方向:)

我会使用DB名称作为键将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添加到窗体或窗体上的容器控件。例如

this.Controls.Add(tagDBname);

您可以循环浏览表单的所有
DataGridView
s,以使用其名称显示预期的表单,同时隐藏其他表单

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

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);
}
private void shownecontrol和hideothers(字符串名称,控件控件),其中T:Control
{
foreach(controls.controls.OfType()中的var控件)
{
control.Visible=control.Name==Name;
}
}
private void ComboxTag_SelectedIndexChanged(对象发送方,事件参数e)
{
//获取应可见的DataGridView的名称:
string selectedTagDB=comboBoxTagDatabases.SelectedItem.ToString();
显示一个控件和隐藏另一个(选择标记数据库,此);
}

可能重复:要显示名为N的DGV并隐藏所有其他DGV吗?@Cid是的,确实是一个很好的解决方案,只需注意
此控件
需要作为DataGridView的父控件。使用下面的字典方法不需要知道父母。谢谢你的回答。您知道如何确定DGV应显示在哪个选项卡上吗?表单具有TabControl布局,DGV不会与此.Controls.Add(tagDBname)一起显示。此要求与您的代码不匹配。如果GridView显示在不同的选项卡页面上,您可以立即使它们全部可见,并且不需要组合框进行选择,因为用户将选择选项卡页面。这将使选定的gridview自动可见。请参阅。创建的所有DGV都支持在一个选项卡上,而不是在其他选项卡上。在初始化DGV时,是否遗漏了DGV应显示在哪个选项卡上的参数?因为它们不显示在表单上。选项卡页有名称。例如:
tabPage1.Controls.Add(tagDBname)。非常感谢,只是我遗漏了一些细节。现在一切都很完美了!:)
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);
}