Solved

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

Posted on 2009-06-29
3
463 Views
Last Modified: 2012-05-07
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?
0
Comment
Question by:FrancineTaylor
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24741269
I've had this in the past as well. What is weird is if I were to put a break point on your GridColumn3 set method, I would see it trigger and the grid value would update. If I did not put a break point, the grid would never refresh.  What solved my problems was to use BindingSource. An example is below, it is a form with only 1 datagridview
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public class GridDataObject
        {
            private string _stateAbbr = "";
            public string StateName { get; set; }
            public string StateAbbr
            {
                get { return _stateAbbr; }
                set
                {
                    this._stateAbbr = value;
                    this.StateName = "STATE [" + value + "]";
                }
            }
        }
 
        public Form1() { InitializeComponent(); }
 
        private List<GridDataObject> list = new List<GridDataObject>();
        private BindingSource bs = new BindingSource();
 
        private void Form1_Load(object sender, EventArgs e)
        {
            list.Add(new GridDataObject() { StateAbbr = "TX" });
            list.Add(new GridDataObject() { StateAbbr = "FL" });
            list.Add(new GridDataObject() { StateAbbr = "CA" });
 
            bs.DataSource = list;
            this.dataGridView1.DataSource = bs;
        }
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 24741657
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.
0
 
LVL 1

Author Closing Comment

by:FrancineTaylor
ID: 31598174
Thanks for a very elegant solution!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question