【发布时间】: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