Solved

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

Posted on 2009-06-29
3
440 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now