动态对象名称

时间:2015-01-08 23:45:20

标签: c# .net for-loop

使用循环最小化此代码?

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;

抱歉不清楚问题,我会尽力解释...... 我想将表添加到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控件。 我知道为什么会出现这个错误,但我不知道如何动态地更改组合名称。

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解这个问题,但是我会对它进行一次攻击...... 让我们从您的错误开始:

  

错误:附加信息:指定的列已属于   一个DataGridView控件。

我怀疑这是因为这两行:

int index = 2;
dataGridView1.Columns.Insert(index, combo);

你是否意味着使用&#34; i&#34;你的for循环?或许你打算在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#动态对象名称

     

使用循环(或其他)最小化此代码?

您可以执行类似

的操作
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});
}

我还没有检查过它的语法,但它应该至少接近。

这就是你想要的吗?

答案 1 :(得分:0)

谢谢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();