Runtime disable datagridviewcombobox

How to change the following elements of a DataGridViewComboBoxColumn at run time:

  • How to set the first value of a combo box by default?
  • Disable the combo box / make it read-only, showing the first default value. The point is, if I have 3 elements in the combo box, it should show only the first element. (Either turn off the combobox or change it to become a text box at run time).

Cause:
The reason I do this is because for my Enum I have Status{New=1,Stop=2,Temp=3} . When I want to register a student, the status is always set to New . So when I save, it will automatically save Status = 1 .

+7
source share
2 answers

Here's how to set the default value and disable the cell:

 using System; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Column1.DataSource = new int[] { 1, 2, 3 }; Column1.DataPropertyName = "Number"; dataGridView1.DataSource = new[] { new { Number=1 }, new { Number=2 }, new { Number=3 }, new { Number=1 } }; } private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == Column1.Index && e.RowIndex == (dataGridView1.Rows.Count - 1)) { DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex]; cell.Value = 2; cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing; cell.ReadOnly = true; } } } } 
+8
source

for the combobox DataGridView control there is SelectedIndex for these articles:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol(v=vs.80).aspx

[one]. Here is how I do it:

 private void dgv_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e) { if (e.Control is DataGridViewComboBoxEditingControl) { DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl; BindingSource bs = control.DataSource as BindingSource; if (!IsNothing(bs)) { // set the filteredChildBS as the DataSource of the editing control ((ComboBox)e.Control).DataSource = filteredChildBS; //Set the dgv combobox to the first item ((ComboBox)e.Control).SelectedIndex = 1 } } 

}

FiltereredChildBS is a binding source, let me know if you need any clarification?

[2]. Disabling the datagridview controls it a little harder. I used this example to disable the DataGridView checkboxes: http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/988c7e3f-c172-467d-89b7-b80a60b7f24f/ , but for comboboxes it’s easiest to disable the bands columns:

 foreach (DataGridViewBand band in dgvTransactions.Columns) { if (i !=7) band.ReadOnly = (bool)i == 0; i+= 1; band.Frozen = false; } 

Let me know if you need further clarification?

0
source

All Articles