Hi ishuhasan21,
Please refer the sample below.
Code
private List<Country> countries;
private ComboBox cmbCountry;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
countries = GetCountries();
BindDataGridView();
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentRow != null && dataGridView1.CurrentCell.ColumnIndex == 0)
{
cmbCountry = e.Control as ComboBox;
if (cmbCountry != null)
{
cmbCountry.SelectedValueChanged += cmbCountry_SelectedValueChanged;
}
}
}
private void cmbCountry_SelectedValueChanged(object sender, EventArgs e)
{
dataGridView1.CurrentRow.Cells[1].Value = dataGridView1.CurrentRow.Cells[0].EditedFormattedValue;
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (cmbCountry != null)
{
cmbCountry.SelectedValueChanged -= cmbCountry_SelectedValueChanged;
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3 && dataGridView1.CurrentRow.Cells[3].Value != null && e.RowIndex == dataGridView1.RowCount - 1)
{
dataGridView1.Rows.Add();
}
}
private List<Country> GetCountries()
{
var countryList = new List<Country> {
new Country {Id=1, Name="USA"},
new Country {Id=2, Name="India"},
new Country {Id=3, Name="Canada"}
};
return countryList;
}
private void BindDataGridView()
{
dataGridView1.CellLeave += dataGridView1_CellLeave;
dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
dataGridView1.AllowUserToAddRows = false;
DataGridViewComboBoxColumn country = new DataGridViewComboBoxColumn();
country.Name = "Country";
country.HeaderText = "Country";
DataGridViewTextBoxColumn city = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn area = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Add(country);
dataGridView1.Columns.Add("State", "State");
dataGridView1.Columns.Add("City", "City");
dataGridView1.Columns.Add("Area", "Area");
((DataGridViewComboBoxColumn)dataGridView1.Columns["Country"]).DisplayMember = "Name";
((DataGridViewComboBoxColumn)dataGridView1.Columns["Country"]).DataSource = countries;
city.HeaderText = "City";
area.HeaderText = "Area";
dataGridView1.Rows.Add();
}
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
Screenshot