0

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);
   }
}
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • If I understand what you are asking, you need to check if the cell that is changed is the one with the combobox. If not then just return. – Crowcoder Mar 01 '18 at 20:44
  • Does this help? https://stackoverflow.com/questions/11141872/event-that-fires-during-datagridviewcomboboxcolumn-selectedindexchanged – Syntax Error Mar 01 '18 at 20:46

1 Answers1

0

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);
        }
    }
JayV
  • 3,238
  • 2
  • 9
  • 14