0

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?

marky
  • 4,878
  • 17
  • 59
  • 103
  • Clean the solution, rebuild, restart. – Crowcoder Jun 23 '20 at 18:21
  • @Crowcoder - no dice: the error didn't go away. – marky Jun 23 '20 at 18:25
  • Does the project still build and run? – Crowcoder Jun 23 '20 at 18:26
  • 1
    In both code blocks you can get the current bound `DataRow` like: `var drCurrRow = dataGridView1.CurrentRow.DataBoundItem as DataRow;`. Also, I think you should handle the `CellFormatting` event instead. –  Jun 23 '20 at 18:26
  • @JQSOFT - I said it worked, but I'm editing this - ran it and drCurrRow is null on the line after that. – marky Jun 23 '20 at 18:29
  • 1
    In short, handle the UI-Related events to control the presentation, and handle the Data-Related events to control the data. –  Jun 23 '20 at 18:32
  • You are trying to cast a DataGridViewRow to a DataRow. Seems you have naming typo. Check your objects names and types. Check this: `dtDefaults.GetType().Name;` –  Jun 23 '20 at 18:34

1 Answers1

1

It turns out it was a simple typo:

If you look closely at the line assigning the variable drCurrRow, in the first code block - the code for the working DGV - I have DataRow drCurrRow = dtXref.Rows[intRowIndex];, but in the second code block, for the second DGV I have DataRow drCurrRow = dgvDefaults.Rows[intRowIndex];. DataRow is a row in a DataTable, a DataGridViewRow is a row in a DataGridView. So JQSOFT's last comment was correct - I was casting a DataGridViewRow as a DataRow, due to a two-letter typo.

marky
  • 4,878
  • 17
  • 59
  • 103