填充datagridview组合框

时间:2013-11-12 22:48:17

标签: c# datagridview

我在Windows窗体上加载了以下代码:

    private void Panou_Load(object sender, EventArgs e)
    {

        List<string>[] list;

        //list in a array with all elements from a select query
        list = Conexiune.Select();


        dataGridView1.Rows.Clear();

        for (int i = 0; i < list[0].Count; i++)
        {
            int number = dataGridView1.Rows.Add();
            dataGridView1.Rows[number].Cells[0].Value = list[0][i];
            dataGridView1.Rows[number].Cells[1].Value = list[1][i];
            dataGridView1.Rows[number].Cells[2].Value = list[2][i];

            dataGridView1.Rows[number].Cells[4].Value = list[4][i];
            dataGridView1.Rows[number].Cells[5].Value = list[5][i];
            dataGridView1.Rows[number].Cells[6].Value = list[6][i];
        }
    }

在我的datagridview上,第4个单元格是一个组合框。如何用我的select(list [3] [i]变量)中的值填充组合框?

更新1:

    private void Panou_Load(object sender, EventArgs e)
    {

        List<string>[] list;

        list = Conexiune.Select();
        dataGridView1.Rows.Clear();


        for (int i = 0; i < list[0].Count; i++)
        {
            int number = dataGridView1.Rows.Add();
            dataGridView1.Rows[number].Cells[0].Value = list[0][i];
            dataGridView1.Rows[number].Cells[1].Value = list[1][i];
            dataGridView1.Rows[number].Cells[2].Value = list[2][i];
            (dataGridView1.Columns[3] as DataGridViewComboBoxColumn).DataSource = new List<string> { list[3][i] };
            dataGridView1.Rows[number].Cells[4].Value = list[4][i];
            dataGridView1.Rows[number].Cells[5].Value = list[5][i];
            dataGridView1.Rows[number].Cells[6].Value = list[6][i];
        }
    }

http://i.stack.imgur.com/0WLDr.jpg

更新2:

    private void Panou_Load(object sender, EventArgs e)
    {

        List<string>[] list;

        list = Conexiune.Select();
        dataGridView1.Rows.Clear();

        (dataGridView1.Columns[3] as DataGridViewComboBoxColumn).DataSource = new List<string> { "", "activ", "inactiv", "neverificat" };

        for (int i = 0; i < list[0].Count; i++)
        {
            int number = dataGridView1.Rows.Add();
            dataGridView1.Rows[number].Cells[0].Value = list[0][i];
            dataGridView1.Rows[number].Cells[1].Value = list[1][i];
            dataGridView1.Rows[number].Cells[2].Value = list[2][i];
            dataGridView1.Rows[number].Cells[3].Value = list[3][i];
            dataGridView1.Rows[number].Cells[4].Value = list[4][i];
            dataGridView1.Rows[number].Cells[5].Value = list[5][i];
            dataGridView1.Rows[number].Cells[6].Value = list[6][i];
        }
    }

http://i.stack.imgur.com/MlnER.jpg

1 个答案:

答案 0 :(得分:5)

为comboBox列设置DataSource

(dataGridView1.Columns[0] as DataGridViewComboBoxColumn).DataSource 
 = new List<string> { "Apples", "Oranges", "Grapes"};    

     for (int i = 0; i < list[0].Count; i++)
    {
        int number = dataGridView1.Rows.Add();
        dataGridView1.Rows[number].Cells[0].Value = list[3][i]; //list[3][1]=="Apples"
    }
}