动态对象名称
dynamic object name
要使用循环最小化此代码?
DataGridViewComboBoxColumn combo_1 = new DataGridViewComboBoxColumn();
string name_1 = "name_1";
string name1 = "Name 1";
combo_1.Name = name_1;
combo_1.HeaderText = name1;
DataGridViewComboBoxColumn combo_2 = new DataGridViewComboBoxColumn();
string name_2 = "name_2";
string name2 = "Name 2";
combo_2.Name = nazwa_2;
combo_2.HeaderText = nazwa2;
DataGridViewComboBoxColumn combo_3 = new DataGridViewComboBoxColumn();
string name_3 = "name_3";
string name3 = "Name_3";
combo_3.Name = name_3;
combo_3.HeaderText = name3;
DataGridViewComboBoxColumn combo_4 = new DataGridViewComboBoxColumn();
string nazwa_4 = "name_4";
string nazwa4 = "Name 4";
combo_4.Name = name_4;
combo_4.HeaderText = name4;
抱歉,问题不明确,我会尽力解释...
我想将 table 添加到 datagridview1
// define combobox column
DataGridViewComboBoxColumn combo_1 = new DataGridViewComboBoxColumn();
string name_1 = "name_1";
string name1 = "Name 1";
combo_1.Name = name_1;
combo_1.HeaderText = name1;
// set value to combobox column
pol.Open();
string list_value = "SELECT value FROM table ORDER BY name ASC";
SqlCommand cmd = new SqlCommand(list_value, conn); //conn is defined above in code
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string name = rdr.GetString(0);
combo_1.Items.Add(name); /
}
pol.Close();
// add all column
dataGridView1.Columns.Add("name_column_1", "Name");
dataGridView1.Columns.Add("name_column_2", "Forname");
int index = 2;
dataGridView1.Columns.Insert(index, combo_1);
//add value to cell
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[index];
cell.Value = dt.Rows[i].ItemArray[index];
对于 "combo_2"、"combo_3" 等。我将不得不做一些仅在“_1”不同的片段
如果我可以使用 for 循环添加 combo_1、combo_2、combo_3
//define combobox
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
for (int i = 1; i <= number_column; i++)
{
string name_1 = "name_" + i;
string name1 = "Name " + i;
combo_1.Name = name_1;
combo_1.HeaderText = name1;
}
//add column
for (int i = 1; i <= liczba_kolumn; i++)
{
int nr = 2;
dataGridView1.Columns.Insert(nr, combo);
nr= nr + 1;
}
//set value
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = dt.Rows[i].ItemArray[0];
dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i].ItemArray[1];
for (int j = 1; j <= number_column; j++)
{
int nr = 2;
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[nr];
cell.Value = dt.Rows[i].ItemArray[nr];
nr = nr + 1;
}
}
错误:附加信息:指定的列已经属于 DataGridView 控件。
我知道为什么会出现这个错误,但我不知道如何动态更改组合名称。
我不确定我是否理解这个问题,但我会尝试一下...
让我们从您遇到的错误开始:
error: Additional information: The specified column already belongs to
a DataGridView control.
我怀疑这是因为这两行:
int index = 2;
dataGridView1.Columns.Insert(index, combo);
您是不是想使用 for 循环中的“i”?或者你可能打算在 for 循环之外声明 index
(我看到你在下面递增它)?
dataGridView1.Columns.Insert(i, combo);
或
int index = 2;
for (int i = 1; i <= number_of_columns; i++)
{
dataGridView1.Columns.Insert(index, combo);
index = index + 1; // or this could be index++;
}
现在让我们看看你的问题的标题和第一行(这是一个不同的主题):
c# dynamic object name
To minimize this code using a loop (or other)?
你可以这样做
List<DataGridViewComboBoxColumn> myColumns = new List<DataGridViewComboBoxColumn>();
for(int i=0; i < (the number you want); i++)
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.Name = "name_" + i;
combo.HeaderText = "Name " + i;
}
这会给你一个(有点)动态列表DataGridViewComboBoxColumn
。但是,如果您打算立即添加它们,您可能更喜欢:
for(int i=0; i < (the number you want); i++)
{
dataGridView1.Columns.Insert(i, new DataGridViewComboBoxColumn{
Name = "name_" + i,
HeaderText = "Name " + i});
}
我还没有检查语法,但它至少应该是接近的。
这是你想要的吗?
感谢PhatWrat,你帮我解决了问题。
pol.Open();
SqlDataAdapter list_adapter = new SqlDataAdapter("SELECT value FROM box ORDER BY value ASC", pol);
DataTable list_dt = new DataTable();
list_adapter.Fill(list_dt);
string[] items = new string[list_dt.Rows.Count];
for (int i = 0; i < list_dt.Rows.Count; i++)
{
items[i] = list_dt.Rows[i][0].ToString();
//MessageBox.Show(list_dt.Rows[i][0].ToString());
}
pol.Close();
int index = 2;
for (int i = 1; i <= number_of_columns; i++)
{
dataGridView1.Columns.Insert(index, new DataGridViewComboBoxColumn
{
Name = "name_" + i,
HeaderText = "Name " + i,
DataSource = items
});
index = index + 1;
}
pol.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM table ORDER BY name ASC", pol);
DataTable dt = new DataTable();
adapter.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = dt.Rows[i].ItemArray[0];
dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i].ItemArray[1];
int index_2 = 2;
for (int j = 1; j <= number_of_columns; j++)
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[index_2];
cell.Value = dt.Rows[i].ItemArray[index_2];
index_2 = index_2 + 1;
}
}
pol.Close();
要使用循环最小化此代码?
DataGridViewComboBoxColumn combo_1 = new DataGridViewComboBoxColumn();
string name_1 = "name_1";
string name1 = "Name 1";
combo_1.Name = name_1;
combo_1.HeaderText = name1;
DataGridViewComboBoxColumn combo_2 = new DataGridViewComboBoxColumn();
string name_2 = "name_2";
string name2 = "Name 2";
combo_2.Name = nazwa_2;
combo_2.HeaderText = nazwa2;
DataGridViewComboBoxColumn combo_3 = new DataGridViewComboBoxColumn();
string name_3 = "name_3";
string name3 = "Name_3";
combo_3.Name = name_3;
combo_3.HeaderText = name3;
DataGridViewComboBoxColumn combo_4 = new DataGridViewComboBoxColumn();
string nazwa_4 = "name_4";
string nazwa4 = "Name 4";
combo_4.Name = name_4;
combo_4.HeaderText = name4;
抱歉,问题不明确,我会尽力解释... 我想将 table 添加到 datagridview1
// define combobox column
DataGridViewComboBoxColumn combo_1 = new DataGridViewComboBoxColumn();
string name_1 = "name_1";
string name1 = "Name 1";
combo_1.Name = name_1;
combo_1.HeaderText = name1;
// set value to combobox column
pol.Open();
string list_value = "SELECT value FROM table ORDER BY name ASC";
SqlCommand cmd = new SqlCommand(list_value, conn); //conn is defined above in code
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string name = rdr.GetString(0);
combo_1.Items.Add(name); /
}
pol.Close();
// add all column
dataGridView1.Columns.Add("name_column_1", "Name");
dataGridView1.Columns.Add("name_column_2", "Forname");
int index = 2;
dataGridView1.Columns.Insert(index, combo_1);
//add value to cell
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[index];
cell.Value = dt.Rows[i].ItemArray[index];
对于 "combo_2"、"combo_3" 等。我将不得不做一些仅在“_1”不同的片段
如果我可以使用 for 循环添加 combo_1、combo_2、combo_3
//define combobox
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
for (int i = 1; i <= number_column; i++)
{
string name_1 = "name_" + i;
string name1 = "Name " + i;
combo_1.Name = name_1;
combo_1.HeaderText = name1;
}
//add column
for (int i = 1; i <= liczba_kolumn; i++)
{
int nr = 2;
dataGridView1.Columns.Insert(nr, combo);
nr= nr + 1;
}
//set value
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = dt.Rows[i].ItemArray[0];
dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i].ItemArray[1];
for (int j = 1; j <= number_column; j++)
{
int nr = 2;
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[nr];
cell.Value = dt.Rows[i].ItemArray[nr];
nr = nr + 1;
}
}
错误:附加信息:指定的列已经属于 DataGridView 控件。 我知道为什么会出现这个错误,但我不知道如何动态更改组合名称。
我不确定我是否理解这个问题,但我会尝试一下... 让我们从您遇到的错误开始:
error: Additional information: The specified column already belongs to a DataGridView control.
我怀疑这是因为这两行:
int index = 2;
dataGridView1.Columns.Insert(index, combo);
您是不是想使用 for 循环中的“i”?或者你可能打算在 for 循环之外声明 index
(我看到你在下面递增它)?
dataGridView1.Columns.Insert(i, combo);
或
int index = 2;
for (int i = 1; i <= number_of_columns; i++)
{
dataGridView1.Columns.Insert(index, combo);
index = index + 1; // or this could be index++;
}
现在让我们看看你的问题的标题和第一行(这是一个不同的主题):
c# dynamic object name
To minimize this code using a loop (or other)?
你可以这样做
List<DataGridViewComboBoxColumn> myColumns = new List<DataGridViewComboBoxColumn>();
for(int i=0; i < (the number you want); i++)
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.Name = "name_" + i;
combo.HeaderText = "Name " + i;
}
这会给你一个(有点)动态列表DataGridViewComboBoxColumn
。但是,如果您打算立即添加它们,您可能更喜欢:
for(int i=0; i < (the number you want); i++)
{
dataGridView1.Columns.Insert(i, new DataGridViewComboBoxColumn{
Name = "name_" + i,
HeaderText = "Name " + i});
}
我还没有检查语法,但它至少应该是接近的。
这是你想要的吗?
感谢PhatWrat,你帮我解决了问题。
pol.Open();
SqlDataAdapter list_adapter = new SqlDataAdapter("SELECT value FROM box ORDER BY value ASC", pol);
DataTable list_dt = new DataTable();
list_adapter.Fill(list_dt);
string[] items = new string[list_dt.Rows.Count];
for (int i = 0; i < list_dt.Rows.Count; i++)
{
items[i] = list_dt.Rows[i][0].ToString();
//MessageBox.Show(list_dt.Rows[i][0].ToString());
}
pol.Close();
int index = 2;
for (int i = 1; i <= number_of_columns; i++)
{
dataGridView1.Columns.Insert(index, new DataGridViewComboBoxColumn
{
Name = "name_" + i,
HeaderText = "Name " + i,
DataSource = items
});
index = index + 1;
}
pol.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM table ORDER BY name ASC", pol);
DataTable dt = new DataTable();
adapter.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = dt.Rows[i].ItemArray[0];
dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i].ItemArray[1];
int index_2 = 2;
for (int j = 1; j <= number_of_columns; j++)
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[index_2];
cell.Value = dt.Rows[i].ItemArray[index_2];
index_2 = index_2 + 1;
}
}
pol.Close();