How to Change background color of the cell if the cell is being edited

193 views Asked by At

I have two datagridview cells, grid1 and grid2. am i loading the files to both grid, i have used the version of the file. every time when i Save/edit a file it increment a file version by 1 from 5, which means it will be 6,7,8,9 etc.

file version starts from 5. Increment it is working with no problem

I want to change the background color of the cell if i loaded the file and edit, from that cell edited, it must change the background color to yellow.

  1. Load existing file
  2. Edit the file
  3. Background color of the edited cell must change to yellow
  4. Save the file and clear the color (this is working)

I have tried this but,it is highlighting the color when i created the file. i only need it to change background cell color only on editing the file.

my code:

    int version_Number = 5;
    string _OriginalValue;
    private void Grid1_CellBeginEdit_1(object sender, DataGridViewCellCancelEventArgs e)
    {
        try
        {
            _OriginalValue = Grid1[e.ColumnIndex, e.RowIndex].Value.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error occured.\nError message: " + ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }


    #region Grid2_CellEndEdit_1
    private void Grid2_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            DataGridViewCell cell = Grid2[e.ColumnIndex, e.RowIndex];
            if (cell.Value.ToString() != _OriginalValue)
            {
                if (version_Number >= 1000)
                {
                    cell.Style.BackColor = Color.Yellow;
                }
            }   
2

There are 2 answers

0
Gilbert Adjin Frimpong On

You have to handle the part that colors the cell in the cellformating event, a sample i tried is as below

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
            if (cell.Value  != null)
            {
                if (cell.Value.ToString() != _OriginalValue)
                {

                    cell.Style.BackColor = Color.Yellow;

                }   
            }

    }
0
TaW On

You may want to use a flag to stop the coloring from happening during the loading of the data:

int version_Number = 5;
string _OriginalValue;

bool loading = false;

You somewhere load the data; now set and reset the flag there:

loding = true;
yourDataLoadingCodeHere;
loading = false;

Now, if they otherwise work you can simply abort your two events:

private void Grid1_CellBeginEdit_1(object sender, DataGridViewCellCancelEventArgs e)
{
    if (loading) return;

    try
    {
        _OriginalValue = Grid1[e.ColumnIndex, e.RowIndex].Value.ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error occured.\nError message: " + 
        ex.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}



private void Grid2_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
   if (loading) return;

   try
   {
     DataGridViewCell cell = Grid2[e.ColumnIndex, e.RowIndex];
     if (cell.Value.ToString() != _OriginalValue)
     {
        if (version_Number >= 1000)
        {
           cell.Style.BackColor = Color.Yellow;
        }
    }  
    ..
    ..