Bac Clunky
Bac Clunky

Reputation: 373

Adding ComboBox to Datagridview

I found a way to add a combobox to DataGridview (Winform) cell, but I have not found an event like ItemDataBound of DataGridView to set a value to comboBox. And do not know how to set a selected value of a comboBox to DataItem property of a current row (of a DataGridView) :(

Please give me some clues to do this task

Thanks you so much

Upvotes: 1

Views: 15687

Answers (2)

Hasanthi
Hasanthi

Reputation: 1271

You can use below method to add data to a combobox in gridview. If you dont have a list you can add items to the combobox as:

cmbdgv.Items.Add("Test");

private void bindDataToDataGridViewCombo() {
    DataGridViewComboBoxColumn cmbdgv = new DataGridViewComboBoxColumn();
    List<String> itemCodeList = new List<String>();
    cmbdgv.DataSource = itemCodeList;
    cmbdgv.HeaderText = "Test";
    cmbdgv.Name = "Test";
    cmbdgv.Width = 270;
    cmbdgv.Columns.Add(dgvCmbForums);
    cmbdgv.Columns["Test"].DisplayIndex = 0;
}

After adding if you want to capture the combobox selection change you can use below event in the datagridview.

ComboBox cbm;
DataGridViewCell currentCell;

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is ComboBox)
    {
        cbm = (ComboBox)e.Control;

        if (cbm != null)
        {
            cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
        }

        currentCell = this.dataGridView1.CurrentCell;
    }
}

void cbm_SelectedIndexChanged(object sender, EventArgs e)
{
    this.BeginInvoke(new MethodInvoker(EndEdit));
}


void EndEdit()
{
    if (cbm != null)
    {
        string SelectedItem=cbm.SelectedItem.ToString();
        int i = dataGridView1.CurrentRow.Index;
        dataGridView1.Rows[i].Cells["Test"].Value = SelectedItem;
    }
}

Upvotes: 4

Kinyanjui Kamau
Kinyanjui Kamau

Reputation: 1986

If you are trying to set the value to a Combobox in a DataGridView, see if this answer will help.

To get the selected item of the Combobox (example):

comboBox.SelectedIndexChanged += new EventHandler(comboBox_ComboSelectionChanged);

private void comboBox_ComboSelectionChanged(object sender, EventArgs e)
      {
            if (myDGV.CurrentCell.ColumnIndex == 5)
            {
                int selectedIndex;
                string selectedItem;

                selectedIndex = ((ComboBox)sender).SelectedIndex;  // handle an error here.
                // get the selected item from the combobox
                var combo = sender as ComboBox;

                if (selectedIndex == -1)
                {
                    MessageBox.Show("No value has been selected");
                }
                else
                {
                    // note that SelectedItem may be null
                    selectedItem = combo.SelectedItem.ToString();

                    if (selectedItem != null)
                    {
                        // Your code

Upvotes: 1

Related Questions