C# Datagridview: get selected item in combobox columns

前端 未结 6 908
遇见更好的自我
遇见更好的自我 2021-01-14 08:46

I\'m working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the us

相关标签:
6条回答
  • 2021-01-14 09:29

    A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.

    EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:

    Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
    string val = box.Text;

    Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.

    0 讨论(0)
  • 2021-01-14 09:33

    Use this to get or set selected value:

    object selectedValue = currentRow.Cells["comboboxColumnName"].Value
    

    Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn

    0 讨论(0)
  • 2021-01-14 09:34

    I use this:

    private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
    {
         return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
    }
    
    0 讨论(0)
  • 2021-01-14 09:41

    The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN

    Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.

    However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).

    You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.

    0 讨论(0)
  • 2021-01-14 09:41

    If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:

    dgcombocell.DisplayMember = "Name"; 
    dgcombocell.ValueMember = "Id";  
    dgcombocell.DataSource = dataset1.Tables[0];
    

    Then for getting SelectedText, and SelectedValue, we can write this code:

    string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
    int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
    

    I hope it solves your problem.

    0 讨论(0)
  • 2021-01-14 09:42

    This is how it is done

      DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];
    
      MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
    
    0 讨论(0)
提交回复
热议问题