Hi Friends..
I need to change the datagridview cell type to combobox while editing. and return back to text column after the editing.
My datagridview is a bounded with list
Here is the sample code
public class Car
{
private string _company;
private string _model;
private int _year;
public string Company
{
get { return _company; }
set { _company = value; }
}
public string Model
{
get { return _model; }
set { _model = value; }
}
public int Year
{
get { return _year; }
set { _year = value; }
}
public Car(string make, string model, int year)
{
_company = make;
_model = model;
_year = year;
}
}
In the form load event
private void Form1_Load(object sender, EventArgs e)
{
List<Car> li=new List<Car>();
li.Add(new Car("Maruthi","Swift",2000));
li.Add(new Car("Maruthi", "Wagnor", 2005));
li.Add(new Car("Maruthi", "Ritz", 2003));
li.Add(new Car("Toyota", "Etios", 2012));
li.Add(new Car("Toyota", "Innova", 2008));
li.Add(new Car("Toyota", "Fortuner", 2010));
dgvcar.DataSource = li;
}
}
private void dgvcar_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int colIndex = dgvcar.CurrentCell.ColumnIndex;
int rowIndex = dgvcar.CurrentCell.RowIndex;
if (colIndex == 1)
{
e.CellStyle.BackColor = Color.Aqua;
DataGridViewComboBoxCell cb = new DataGridViewComboBoxCell();
cb.Items.Add("Etios");
cb.Items.Add("Innova");
cb.Items.Add("Fortuner");
dgvcar[colIndex, rowIndex] = cb;
}
}