1

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. :)

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Brainfail
  • 196
  • 1
  • 2
  • 11

1 Answers1

1

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";
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • oh haha :D its a nice property. it works fine, thank you :) its a better way than i solved it by myself. – Brainfail Nov 29 '18 at 07:16