Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 26th, 2006, 11:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Object Bound DataGrid Update Issue

Hi All,
I have a windows datagridview that I am having some issues with.
The problem is that after the grid is bound to the object datasource it does not seem to reflect changes to the base datasource.
How do I refresh the grid to show the true dataset from the object.
Below is the test code I have used to try to get this to work.
The datasource is populated in the form constructor and bound to the grid.
Button one add new data to the data source.(I thought this should be all that was needed.)
Button two is trying to rebind, but not sure what to do here.

Code:
public partial class Form1 : Form
    {
        People people = new People();//List of person objects
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 5; i++)
            {
                Person p = new Person();
                p.FirstName = "Rod_" + i.ToString();
                p.LastName = "McLeay_" + i.ToString();
                people.Add(p);
            }
            dataGridView1.DataSource = people;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Person p = new Person();
            p.FirstName = "Rod_" + people.Count.ToString();
            p.LastName = "McLeay_" + people.Count.ToString();
            people.Add(p);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = people;
            dataGridView1.Refresh();
        }

    }
======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old January 8th, 2007, 11:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Does anyone know how to rebind a object bound control?
There does not seem to be a Bind() method???

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old January 29th, 2007, 04:49 PM
Registered User
 
Join Date: Jan 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

try this:
        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            dataGridView1.Refresh();

            dataGridView1.DataSource = people;
            dataGridView1.Refresh();
        }
I found it here: http://www.csharpfriends.com/Article...x?articleID=81

but I didn't test it yet, since I am having problems with controls created on a thread and parented by another, but I guess in your case it should work.

 
Old February 20th, 2007, 02:18 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prsnt_mhatre
Default

refresh datagridview and display current record in datagridview

 
Old February 22nd, 2007, 04:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks heaps had1980, much appreciated.

======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old February 24th, 2007, 10:04 PM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm having pains with this as well. My background is with asp, so I'm still learning the variations for desktop apps...
I had discovered the fix of setting the datasource to null and then to the appropriate source to clear it (I did not have to call refresh). I ended up setting it to an empty list instead, because I had defined columns and found that if I set the datasource to null, those definitions are wiped out and the grid re-generates them (showing fields I want hidden).

However, now the data shows, but clicking on a row (to select it) generates a cryptic error "Index -1 does not have a value.".
Any advice for getting around this one? (Or for that matter, is anyone else needing this fix seeing the same problem?) I need users to be able to select rows...

 
Old February 24th, 2007, 10:44 PM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Apparently I posted too soon.
I found a solution at http://www.tutorials-se.com/windowsforms/Index-does/ - not exactly the answer I was hoping for, but it works and is clean :).

Instead of binding a list directly to the grid, add a BindingSource to your form, and set the grid's datasource to it. Then you just need to assign your list to the BindingSource's datasource once. Any time you need to refresh the grid, call ResetBindings(false) from your BindingSource object.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting Gridview bound to strongly typed object kulkarnimonica ASP.NET 2.0 Professional 10 September 20th, 2007 03:32 PM
Bound Template Column in DataGrid dginley ASP.NET 1.0 and 1.1 Professional 2 November 4th, 2006 03:17 PM
Bound Object Frame Property Needed SerranoG Access VBA 0 April 16th, 2004 10:42 AM
Bound Controls don't update SQL DB Ron Howerton ADO.NET 1 January 28th, 2004 03:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.