c# add a dgv row with a dataGridViewComboBoxCell

前端 未结 3 1486
礼貌的吻别
礼貌的吻别 2021-01-27 00:34

I\'m currently trying to add a ComboBox to a dataGridView.

In the DGV, there are 5 columns: checkbox, string, string, combobox, combobox.

both combobox-columns a

相关标签:
3条回答
  • 2021-01-27 01:23

    If you need to keep configured columns try something like this:

    dataGridView1.Rows.Add();
    DataGridViewRow tmp = dataGridView1.Rows[dgvMatw.Rows.Count - 1];
    tmp.Cells[0] = true;
    tmp.Cells[1] = "str1";
    tmp.Cells[2] = "str2";
    ((DataGridViewComboBoxCell)tmp.Cells[3]).Value = 1;
    ((DataGridViewComboBoxCell)tmp.Cells[4]).Value = 2;
    

    Values 1 and 2 where example only. It must be type of your DataGridViewComboBoxColumn.ValueMember. I was searching for this topic, so I thought someone could use it.

    0 讨论(0)
  • 2021-01-27 01:24

    You can use this solution for adding items to datagird view combobox column

     DataSet ds; //class variable
        public Form1()
        {
            InitializeComponent();
    
            ds = new DataSet();
            //column 1 (normal textColumn):
            dataGridView1.Columns.Add("col1", "Column1");
            //column 2 (comboBox):
            DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
            comboCol.Name = "cmbColumn";
            comboCol.HeaderText = "combobox column";
            dataGridView1.Columns.Add(comboCol);
    
            //using dataTable for each datasource:             
            for (int i = 0; i < 10; i++)
            {
                string text = "item " + i; //data for text
                int[] data = { 1 * i, 2 * i, 3 * i }; //data for comboBox:
    
                //create new dataTable:
                DataTable table = new DataTable("table" + i);
                table.Columns.Add("column1", typeof(string));
    
                //fillig rows:
                foreach (int item in data)
                    table.Rows.Add(item);
    
                //add table to dataSet:
                ds.Tables.Add(table);
    
                //creating new row in dgv (text and comboBox):
                CreateCustomComboBoxDataSouce(i, text, table);
            }
        }
    
        private void CreateCustomComboBoxDataSouce(int row, string texst, DataTable table) //row index ,and two parameters
        {
            dataGridView1.Rows.Add(texst);
            DataGridViewComboBoxCell comboCell = dataGridView1[1, row] as DataGridViewComboBoxCell;
            comboCell.DataSource = new BindingSource(table, null);
            comboCell.DisplayMember = "column1"; //name of column indataTable to display!!
            comboCell.ValueMember = "column1"; // vlaue if needed 
            //(mostly you used these two propertes like: Name as DisplayMember, and Id as ValueMember)
        }
    

    if the above is not working take a look at the solution below..

    you can do it via DataGridViewComboBoxCell. According to the cell's rowIndex, set different datasource(string[]) to the different DataGridViewComboBoxCell. Coding like this:

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                DataGridViewComboBoxCell combo = this.dataGridView1[0, e.RowIndex] as DataGridViewComboBoxCell;
    
                    if (e.RowIndex == 0)
                    {
                        //these data will be displayed in comboBox:
                         string[] data= {"item A1", "item B1", "item C1"};  
                        combo.DataSource = data;
                    }
                    if (e.RowIndex == 1)
                    {
                        //these data will be displayed in comboBox:
                        string[] data = {"item A2", "item B2", "item C2"};                        
                        combo.DataSource = data;
                    }
                    if (e.RowIndex == 2)
                    {
                        //these data will be displayed in comboBox:
                        string[] data = { "item A3", "item B3", "item C3" };                           
                        combo.DataSource = data;
                    }
    
                }
            }
        }    
    
    0 讨论(0)
  • 2021-01-27 01:34

    I'm not entirely clear what you're trying to do here, but if you're just trying to choose a value from the combobox for each row you add, you can choose an existing combobox value as a string.

    If I have a two column datagridview, both with comboboxes that are pre-populated (I populated my comboboxes in the datagridview control itself by simply editing each column and adding my choices to the collection there), then I can do this:

    Public Class Form1
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim newRow(1) As Object
            newRow(0) = "Foo"
            newRow(1) = "Bar"
            DataGridView1.Rows.Add(newRow)
        End Sub
    End Class
    

    So "Foo" and "Bar" are already choices in the comboboxes. I can populate each row by simply referring to the choice I want by name. I would guess I could also do this by index number if I wanted to.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题