Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-06-29
3
Medium Priority
?
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 2000 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

610 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