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
Solved

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

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

861 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