I have a simple datagridview with 2 columns (Column1 and Column2). Cells in Column1 has a TextBox and cells in Column2 has a ComboBox with two color options (Blue or Red)
First, I wanted to change the datagridview row color when selecting a color in the combobox in that row, and I did it great with the CellValueChanged and CurrentCellDirtyChanged event. No problem.
But now, I want to add a YesNo Message.Box to confirm the color change. It works ok when I confirm changes (cliking 'Yes') but fails when I do not confirm it (clicking 'No').
I tried the following code:
Public Class Form1
Dim lastOption As String = "Blue"
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.RowIndex > -1 Then
Dim oldOption = DataGridView1.Rows(e.RowIndex).Cells("Column2").Value
If e.ColumnIndex = 1 Then
Dim optionChoosen As String = DataGridView1.Rows(e.RowIndex).Cells("Column2").Value.ToString
Dim result = MessageBox.Show(" Are you sure you want to set this option?", "Are you sure?", MessageBoxButtons.YesNo)
If optionChoosen = "Red" Then
If result = DialogResult.Yes Then
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.White
lastOption = optionChoosen
Else
DataGridView1.Rows(e.RowIndex).Cells("Column2").Value = lastOption
End If
ElseIf optionChoosen = "Blue" Then
If result = DialogResult.Yes Then
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Blue
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.White
lastOption = optionChoosen
Else
DataGridView1.Rows(e.RowIndex).Cells("Column2").Value = lastOption
End If
End If
End If
End If
End Sub
Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
If (DataGridView1.IsCurrentCellDirty) Then
' This fires the cell value changed handler below
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
End Class
When I select a color in the combobox and I confirm it, the row color changes properly. But when I do not confirm it, I do not know how to 'remember' the last selected combobox option for that combobox, to use it in the 'Else' branch of the CellValueChanged event handler:
- As you can see, I keep the last selected combobox value in a 'lastOption' variable out of the CellValueChanged method.
- When the message box is launched and I choose 'NO', it goes to the 'Else' branch to assign the 'lastOption' value to the current combobox selection (I had to select one to trigger the MessageBox)
I know this code is not useful because I should have to use 'n' variables to remember 'n' combobox values (one for every row) but I use it just for a single row testing.
If you try this code, you will see that even for one row it doesn't work because the message box is triggered twice (it seems, not sure, once for combobox selection and other when changing value of the combo programatically in the 'Else' branch).
If anyone can help me to solve it, I would be very grateful. Thanks for your time!