DataGridViewComboBoxColumn name/value how?

前端 未结 2 1943
挽巷
挽巷 2020-12-06 06:57

I thought this was simple like in Access.

User needs to set the value of one column in a datatable to either 1 or 2.

I wanted to present a combobox showing \

2条回答
  •  失恋的感觉
    2020-12-06 07:29

    This is how you read the value from the grid when the value in the combobox changes:

    dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
    
    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
        {
            ComboBox comboBox = e.Control as ComboBox;
            comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
        }
    }
    
    private void LastColumnComboSelectionChanged(object sender, EventArgs e)
    {
        var sendingCB = sender as DataGridViewComboBoxEditingControl;
        object value = sendingCB.SelectedValue;
        if (value != null)
        {
            int intValue = (int)sendingCB.SelectedValue;
            //do something with value
        }
    }
    

    sources: this post

提交回复
热议问题