?
Solved

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

Posted on 2009-06-29
3
Medium Priority
?
493 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

752 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