调用并将数据项添加到datagridview中新的组合框

时间:2019-08-03 13:04:51

标签: c# datagridview combobox datagridviewcomboboxcell

在我的程序中,我在datagrid视图的前两行中有一些带有可变编号(取决于用户选择)的组合框。现在我想添加项目并在其他类中与它们一起使用,但是由于我不知道如何在创建之前调用它们(我是在运行时创建的)而无法访问它们

//这是我创建它们的方式:

for (int j = 0; j < columncount; j++) {
    dataGridView1.Rows[i].Cells[j] = new DataGridViewComboBoxCell();
}

1 个答案:

答案 0 :(得分:0)

关于线路代码的演示

  public Form0114()
        {
            InitializeComponent();
            Combox();
        }
        public void Combox()
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";
            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);
            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            cmb.HeaderText = "Select Data";
            cmb.Name = "cmb";
            cmb.MaxDropDownItems = 4;
            cmb.Items.Add("True");
            cmb.Items.Add("False");
            dataGridView1.Columns.Add(cmb);
        }
相关问题