im want put a DataGridViewComboBoxCell
in a specific column for any rows i have in my DataGridView
.
Now i have a problem... I would like to show a default value in that specific columns where i had put a DataGridViewComboBoxCell
, but there is nothing shown..
String[] options = new String[] {"On", "Off" };
for(int i = 0; i< rows.Count; i++)
{
// lets say one row looks like this {"1","On"}, {"2", "Off"}
MydataGridView.Rows.Add(rows[i]);
DataGridViewComboBoxCell MyComboBox= new DataGridViewComboBoxCell();
MyComboBox.Items.AddRange(options);
MydataGridView.Rows[i].Cells[1] = MyComboBox;
}
I would like to show my added rows as default, before I can select a {"On","Off"}
state via DataGridViewComboBoxCell
.
I hope it was clearly enough. :)
What about this (DataGridView.CellFormatting Event
):
public Form1()
{
InitializeComponent();
MydataGridView.CellFormatting += MydataGridView_CellFormatting;
}
private void MydataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1) //Index of your DataGridViewComboBoxCell
{
e.Value = "yourDefaultValue";
}
}
Also I have seen the following event as well, that is also used to set the default value (DataGridView.DefaultValuesNeeded Event
):
public Form1()
{
InitializeComponent();
MydataGridView.DefaultValuesNeeded += MydataGridView_DefaultValuesNeeded;
}
private void MydataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[1].Value = "yourDefaultValue";
}