How to get the TEXT of Datagridview Combobox selected item?

前端 未结 5 792
渐次进展
渐次进展 2021-02-04 14:04

How to get the Combobox selected item text which is inside a DataGridView? I have tried using the below code:

dataGridView1.Rows[1].Cells[1].Val         


        
相关标签:
5条回答
  • 2021-02-04 14:44

    To access the currently selected text in the datagridview, you need a reference to the CurrencyManager of the Combobox column. The CurrencyManager has nothing to do with money but instead manages the binding between the the column and it's datasource. The CurrencyManager can tell you what the current selection of the combobox is.

    Teh codes:

        CurrencyManager cm = (CurrencyManager)DataGridView1.BindingContext[((System.Windows.Forms.DataGridViewComboBoxColumn)DataGridView1.Columns[0]).DataSource];
    

    Note: it is not necessary to cast the column to a combobox, i just did that to show you what column I was passing in. I used index 0 but use whatever index is the actual index of your combobox column.

    Now using the currency manager you can access the current selection of the datagrid for that column (because that was the column you passed in).

        cm.Current; //returns the current selection whatever that is
    

    So in my case the datasource of the combobox column was a class called Choice, choice looks like this:

        public class Choice
        {
                public string Text
                {
                    get;
                    set;
                }
        }
    

    When I access the cm.Current property it will return an instance of the choice class, I can now access the Text property of my choice class to see what value was selected. You will obviously have to adapt this to your particular situation. I hope this helps.

    0 讨论(0)
  • 2021-02-04 15:00

    Also try this:

    DataGridView.CurrentRow.Cells(Column.name).EditedFormattedValue.ToString()
    

    EditedFormattedValue object gives the current, formatted value of the cell in DataGridView, regardless of the cell is edited or in edit mode. It's more convenient to capture the ComboBox selection or any value of cell in DataGridView.

    0 讨论(0)
  • 2021-02-04 15:01

    I managed to pull that string value out of the cell this way:

    DataGridViewComboBoxCell dgvcmbcell = dataGridView1[1, 0] as DataGridViewComboBoxCell;
    String text = dgvcmbcell.EditedFormattedValue.ToString();
    

    Easiest way to figure this out is use the debugger and look into the dgvcmdcell object. In this you will find the expandable node "base". Expand it and just look through it and you will find whatever information you need.

    0 讨论(0)
  • 2021-02-04 15:03

    To get selected value and selected text of Combobox in DataGridView try following Code

    string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
    int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
    
    0 讨论(0)
  • 2021-02-04 15:10

    You could Try this :-

    dataGridView1.CurrentRow.Cells[0].Value.ToString();
    

    Index the column of the cell you need to look at, whichever is the index of your ComboBoxColumn.

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