如何将datagridview列标题文本添加为组合框项C#。。?
我有一个datagridview,在form1中有多列,而我的form2包含许多comboboxs。在这里,我想添加(form1)datagridview列标题文本作为(form2)组合框项 我已经在form2中试过了如何将datagridview列标题文本添加为组合框项C#。。?,c#,winforms,datagridview,combobox,C#,Winforms,Datagridview,Combobox,我有一个datagridview,在form1中有多列,而我的form2包含许多comboboxs。在这里,我想添加(form1)datagridview列标题文本作为(form2)组合框项 我已经在form2中试过了 try { Form1 F1 = new Form1(); comboBox1.Items.Add(F1.dataGridView1.Columns[0].HeaderText); comboBox1.Items.Add(F1.dataGridView1.Colum
try
{
Form1 F1 = new Form1();
comboBox1.Items.Add(F1.dataGridView1.Columns[0].HeaderText);
comboBox1.Items.Add(F1.dataGridView1.Columns[1].HeaderText);
comboBox1.Items.Add(F1.dataGridView1.Columns[2].HeaderText);
comboBox1.Items.Add(F1.dataGridView1.Columns[3].HeaderText);
comboBox1.Items.Add(F1.dataGridView1.Columns[4].HeaderText);
comboBox1.Items.Add(F1.dataGridView1.Columns[5].HeaderText);
}
catch{}
但是没有用。。有人能帮我一下吗。是的,这可以通过将DataGridView的访问修饰符从Private更改为Public来实现。我已经做了实际操作,效果很好
public System.Windows.Forms.DataGridView dataGridView1;// at your form1
是的,这可以通过将DataGridView的访问修饰符从Private更改为Public来实现。我已经实际做到了,而且效果很好
public System.Windows.Forms.DataGridView dataGridView1;// at your form1
一种方法(使用封装)是将此函数添加到F1
表单中:
public string GetDGVHeaderText(int index)
{
if (index < dataGridView1.ColumnCount)
{
return dataGridView1.Columns[index].HeaderText;
}
else
{
return string.Empty;
}
}
现在,您可以在F2
中存储对第二个表单的引用,或者在构造函数体中使用它
例如:
public F2(F1 f1Ref)
{
InitializeComponent();
comboBox1.Items.Add(f1Ref.GetDGVHeaderText(0));
comboBox2.Items.Add(f1Ref.GetDGVHeaderText(1));
}
一种方法(使用封装)是将此函数添加到F1
表单中:
public string GetDGVHeaderText(int index)
{
if (index < dataGridView1.ColumnCount)
{
return dataGridView1.Columns[index].HeaderText;
}
else
{
return string.Empty;
}
}
现在,您可以在F2
中存储对第二个表单的引用,或者在构造函数体中使用它
例如:
public F2(F1 f1Ref)
{
InitializeComponent();
comboBox1.Items.Add(f1Ref.GetDGVHeaderText(0));
comboBox2.Items.Add(f1Ref.GetDGVHeaderText(1));
}
我认为更改整个类/窗体访问修饰符是一种糟糕的做法。它可能会根据您的需要进行调整,但通常我会尽量避免这种情况。我认为更改整个类/窗体访问修饰符是一种不好的做法。这可能取决于你的需要,但通常我会尽量避免。