I am beginner In C#. I am working on Datagridview, Data of Gridview is coming from SQL table and i have made a ComboboxColumn and added some values in it. I want if i select any value from Dropdown of ComboboxColumn it should be shown on only one specified column without reflecting other columns... I am trying below code. This code is working but when i change values of other columns the one specified column value is also changing which i want should only be done with ComboboxColumn.
private void ViewDataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
ViewDataGrid.Rows[e.RowIndex].Cells["Arrival_State"].Value =
ViewDataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Before you change the value of the Arrival_State column, you should check to see if the column that was changed is the ComboBoxColumn. See my code below
I added two if blocks, use one that you like the look of and not both.
private void ViewDataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
// Use this method if you want to find/specify the column by its index number
// Replace 1 with the index of the ComboBoxColumn
if (e.ColumnIndex == 1)
{
ViewDataGrid.Rows[e.RowIndex].Cells["Arrival_State"].Value =
ViewDataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
}
// Use this method if you want to find/specify the column by its name
// I prefer this method just in case the order changes
if (ViewDataGrid.Columns[e.ColumnIndex].Name == "ComboBoxColumnName")
{
ViewDataGrid.Rows[e.RowIndex].Cells["Arrival_State"].Value =
ViewDataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}