1

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

Bac Clunky
  • 353
  • 3
  • 6
  • 18
  • have you tried anything for it ? – Dhaval Patel Jul 21 '14 at 11:27
  • do you want to add combo box in datagridview – Sathish Jul 21 '14 at 11:35
  • @DhavalPatel yes, I did but no luck – Bac Clunky Jul 22 '14 at 01:21
  • @Sathish Yes, I meant it. I want to have a comboBox in every row of DataGridView. I want to set comboBox value when item data bound event (not found this built-in event or a similar one, please help), and every time when user select an item of a comboBox, the selected item will be assigned to a data item property – Bac Clunky Jul 22 '14 at 01:23
  • set which value to combo box? – Sathish Jul 22 '14 at 04:31
  • @Sathish for example: comboBox contains 2 values (Males(value 1), Female(value 2)...if the person X is male, the comboBox will select Male. If user selects Female in comboBox, the value 2 must be apply for that person – Bac Clunky Jul 22 '14 at 05:54
  • if you select the value it assign in that cell right? – Sathish Jul 22 '14 at 06:01
  • @Sathish yes, and when list data, every combox box must select an appropriate value of that cell as well – Bac Clunky Jul 22 '14 at 06:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57732/discussion-between-sathish-and-bac-clunky). – Sathish Jul 22 '14 at 06:20

2 Answers2

4

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;
    }
}
sampathsris
  • 21,564
  • 12
  • 71
  • 98
Hasanthi
  • 1,251
  • 3
  • 14
  • 30
1

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
Community
  • 1
  • 1
Kinyanjui Kamau
  • 1,890
  • 10
  • 55
  • 95