Changing colors in WinForm DataGridView on keypress

106 Views Asked by At

I have three columns in a DataGridView that are mutually dependent on each other. Lets label them column 1, 2, and 3 respectively. In this case column 1 must be less than column 2 and column 2 must be less than column three.

If I call the SetValidBandByRow() function below while in the sysIdentGV_CellValueChanged the cell colors are the expected Light Pink to indicate that there is an error in one of the three cells and needs to be addressed. However when SetValidBandByRow(...) is called from a KeyPress event it registers that a new value violates the condition but the cell that is being edited does not retain the cell colors set in SetValidBandByRow(...). In an attempt to fix this I have caught that the KeyPress by setting the textbox backcolor to Light pink however I get a white boarder see the "while editing" image below.

private void sysIdentGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    /*some other validation and building of the rltu object here */

    if(!SetValidBandByRow(rltu))
    {
        /*building error msg and displaying error*/
    }
    return;
}

private bool SetValidBandByRow(Tuple<int,double,double,double> rowLowerTransUpper)
{
    bool wasValid = true;

    if (rowLowerTransUpper.Item1 < 0 || rowLowerTransUpper.Item1 >= GV.RowCount)
        return wasValid;

    Color color = Color.White;
    if (!(rowLowerTransUpper.Item2 <= rowLowerTransUpper.Item3 && rowLowerTransUpper.Item3 <= rowLowerTransUpper.Item4))
    {
        color = Color.LightPink;
        wasValid = false;
    }

    GV.Rows[row].Cells[(int)GVEnum.lowerCutoff].Style.BackColor = color;
    GV.Rows[row].Cells[(int)GVEnum.TransFreq].Style.BackColor = color;
    GV.Rows[row].Cells[(int)GVEnum.upperCutoff].Style.BackColor = color;

    return wasValid;
}

Setting tb.backcolor on GV_Keypress (Cell is still in focus):

while editing

Not setting tb.backcolor on GV_Keypress (Cell is still in focus):

while editing

after setting GV.Rows[row].Cells[(int)GVEnum.lowerCutoff].Style.BackColor and SelectionBackColor on GV_CellValueChanged:

after defocusing from textbox

I am hoping to either remove the white space around the "while editing" image or find another approach that seems like less of a workaround.

This is some things that I have tried to set the textbox back color but nothing seems to be working as I would expect:

private void GV_KeyPress(object sender, KeyPressEventArgs e)
{

/*some other validation and building of the rltu object here */

//the result of the section below can be seen in image 1 "Setting 
//active textbox back color" with out this section I get image two.

TextBox tb = (TextBox)GV.EditingControl;
if (!SetValidBandByRow(rltu))
{
    tb.Margin = new Padding(0);
    GV.CurrentCell.Style.BackColor = Color.LightPink;
    GV.CurrentCell.Style.SelectionBackColor = Color.LightPink;
    tb.BackColor = Color.LightPink;
}
else
    tb.BackColor = Color.White;
}

Thanks

1

There are 1 best solutions below

0
Siberian On

If anyone else is looking to do something similar. The question was answered in another stack over flow question that can be found here