In a C# WinForms project I have two DGVs with similar functionality, just different data.
For each, when the user changes the value of editable cells I'm using CellValueChanged
to Change the color of the cell if what the user typed is different than the value that was in the cell.
Here's the code for that. Everything works as needed & intended.
private void dgvXref_CellValueChanged(Object sender, DataGridViewCellEventArgs e)
{
int intRowIndex = dgvXref.CurrentCell.RowIndex;
int intColIndex = dgvXref.CurrentCell.ColumnIndex;
DataRow drCurrRow = dtXref.Rows[intRowIndex];
string strOriginalValue = drCurrRow[intColIndex, DataRowVersion.Original].ToString();
string strEnteredValue = drCurrRow[intColIndex, DataRowVersion.Proposed].ToString();
if (strOriginalValue != strEnteredValue)
{
dgvXref.Rows[intRowIndex].Cells[intColIndex].Style.BackColor = Color.Yellow;
}
else
{
dgvXref.Rows[intRowIndex].Cells[intColIndex].Style.BackColor = Color.Empty;
}
}
I need to do the same thing for the other DGV as well, so I just copy/pasted the code and changed the DGV's name:
private void dgvDefaults_CellValueChanged(Object sender, DataGridViewCellEventArgs e)
{
int intRowIndex = dgvDefaults.CurrentCell.RowIndex;
int intColIndex = dgvDefaults.CurrentCell.ColumnIndex;
DataRow drCurrRow = dgvDefaults.Rows[intRowIndex];
string strOriginalValue = drCurrRow[intColIndex, DataRowVersion.Original].ToString();
string strEnteredValue = drCurrRow[intColIndex, DataRowVersion.Proposed].ToString();
if (strOriginalValue != strEnteredValue)
{
dgvDefaults.Rows[intRowIndex].Cells[intColIndex].Style.BackColor = Color.Yellow;
}
else
{
dgvDefaults.Rows[intRowIndex].Cells[intColIndex].Style.BackColor = Color.Empty;
}
}
My problem is, for the second DGV, dgvDefaults.Rows[intRowIndex]
has a red error line under it, saying, "Cannot implicitly convert type 'System.Windows.Forms.DataGridViewRow' so 'System.Data.DataRow'. Which totally confuses me, since I'm doing the same thing on the other DGV, but there's no error and it runs fine. The DataSets for each of the DGVs is set up the same and the DGVs' properties are the same - the only difference is the data they hold.
I tried changing the second DGV's drCurrRow
code to DataGridViewRow drCurrRow = dgvDefaults.Rows[intRowIndex];
and that doesn't have an error on it, but I can't figure out how to get the Original and Proposed version using that.
A) Why am I getting an error on that line for the second DGV but not the first and B) How can I accomplish my need for this?