Link to home
Start Free TrialLog in
Avatar of FrancineTaylor
FrancineTaylor

asked on

DataGridView: manual change in databound object doesn't trigger redisplay of value in grid

I have two State columns in my DataGridView, the first is the abbreviation (example: "TX") and the second is the name (example: "Texas").  The short name column is a combo box column, the second is a display-only text.

In the class which is bound to the grid, here are the state properties:

public string GridColumn3 {
    get { return column3; }
    set {
        // if we are changing the state
        if (!value.Equals(this.column3)) {
            state = cState.FindState(value.ToString());  // returns a state object
            this.column3 = state.ShortName;
            this.column4 = state.LongName;
        }
    }
}
public string GridColumn4 {
    get { return column4; }
}

Here is the problem; the value in column4 is changed by the column3 property, but that change doesn't show up visually in the grid until the user enters the cell.  How can I force the column4 cell to redisplay after the column3 property change occurs?
ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FrancineTaylor
FrancineTaylor

ASKER

Thanks, wht1986, that did work.  Very interesting.  I wonder what all the differences might be between binding directly to the list and binding to a binding source bound to the list?

In any case, just FYI, I just Googled a post which had this suggestion:

// This forces the row to repaint itself after any value is changed in the row
int oldRowIndex = -1;
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e) {
    if (oldRowIndex != -1) {
        this.dataGridView1.InvalidateRow(oldRowIndex);
    }
    oldRowIndex = this.dataGridView1.CurrentCellAddress.Y;
}

...which also works, but your solution is much more efficient and elegant.
Thanks for a very elegant solution!