Binding a DataGridView in code

  • Thread starter Thread starter GaryDean
  • Start date Start date
G

GaryDean

Most of my .net experience is in ASP.Net. I don't understand why all of the
dataaccess has to be different in forms but anyway...

I have a DataGridView connected to a data source and I see it automatically
binds (I don't like that). Now I have added more records to the datasrouce
and I find that there is no DataBind method. I know this sounds like a
stupid question but how can I get this grid to re-bind so it will show the
new data?
 
Hi Gary,

The programming mode of WinForms applications is different from that of the
Web applications, including the data binding side.

When we bind a DataGridView to a data source in a WinForms application, the
columns and rows will be populated in the DataGridView automatically. If
you don't like the columns to be populated automatically, you could set the
AutoGenerateColumns property of the DataGridView to false before you bind
the DataGridView to a data source.

DataGridView doesn't have a method called DataBind. In fact, DataGridView
needn't have this method. When there're changes in the data source, e.g.
new data is added in the data source, DataGridView will show the new data
automatically without any code.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Gary,
If you're dgv is not showing the new records, you might try the Refresh
method or the Invalidate method.

Barry
 
Linda,
No, that does not work. The DataGridView does NOT automatically update
itself when records are inserted into the datasource. Nor does the Refresh
or Invalidate methods cause this to happen.

I need the AutoGenerateColumns set to generate Columns because I want to
specify specific attributes such as width, etc at design time.

So, my question stands: How can I get the DataGridView to ReBind after
records have been inserted? Is there any methods that can make this happen.
 
Barry,
No, that does not work. The DataGridView does NOT automatically update
itself when records are inserted into the datasource. Nor does the Refresh
or Invalidate methods cause this to happen.

So, my question stands: How can I get the DataGridView to ReBind after
records have been inserted? Is there any method that can make this happen.
 
BTW, I also tried every combination of the following:
gvOrders.DataSource = null;
gvOrders.Refresh();
gvOrders.Invalidate();
gvOrders.DataSource = getPOInfoBindingSource;

Nothing works. I think the DataGridView really needs a databind method.
 
BTW, I also tried every combination of the following:
gvOrders.DataSource = null;
gvOrders.Refresh();
gvOrders.Invalidate();
gvOrders.DataSource = getPOInfoBindingSource;

Nothing works. I think the DataGridView really needs a databind method.
 
GaryDean schreef:
Now I have added more records to the datasrouce
and I find that there is no DataBind method. I know this sounds like a
stupid question but how can I get this grid to re-bind so it will show the
new data?

Exactly what kind of datasource are you using? BindingList<T> seems like
a good idea..

Anyway, you seem to have tried a couple of methods, but i didn't see you
mention the 'ResetBindings' method...
 
Hi Gary,

Thank you for your response.

Could you tell me what kind of data source you're using? I guess it is a
list of custom objects, right?

Only those data source that implements the IBindingList interface can
support change notification for when the list itself changes(for example,
the number of items in the list increases or decreases). BindingList<T>
provides a generic implementation of the IBindingList interface, so it is a
good candidate to use as the data source.

Note that a class must implement the INotifyPropertyChanged interface to
make change notification when any of its property values change.

To summary, I suggest that you use the BindingList<T> as the data source
and implement the INotifyPropertyChanged interface for your business class.
I will illuminate this with a sample. It requires that you add a
DataGridView and two Buttons on the form.

public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
BindingList<Person> lists = new BindingList<Person>();
lists.Add(new Person("1", "aa"));
lists.Add(new Person("2", "bb"));
this.dataGridView1.DataSource = lists;
}
private void button1_Click(object sender, EventArgs e)
{
BindingList<Person> lists = this.dataGridView1.DataSource as
BindingList<Person>;
lists.Add(new Person("3", "22"));
}
private void button2_Click(object sender, EventArgs e)
{
BindingList<Person> lists = this.dataGridView1.DataSource as
BindingList<Person>;
lists[1].ID = "5";
}
class Person:INotifyPropertyChanged
{
string id;
string name;
public string ID
{
get { return id; }
set
{
id = value;
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs("ID"));
}
}
public string Name
{
get { return name; }
set
{
name = value;
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs("Name"));
}
}
public Person(string _id,string _name)
{
id = _id;
name = _name;
}
public event PropertyChangedEventHandler PropertyChanged;

}
}

Run the program. When you click the button1, you should see a new row is
added in the DataGridView. When you click the button2, you should see the
value of column 'ID' in the second row is changed to 5.

For more information on interfaces related to data binding, you may read
the following MSDN document:
'Interfaces Related to Data Binding '
http://msdn2.microsoft.com/en-us/library/41e17s4b.aspx

BTW, the Refresh or Invalidate method is used to cause the DataGridView to
be redrawn. It's no use calling either of these two methods to get the
DataGridView to show new data in the data source.

Alternatively, if you're using BindingSource as the data source, i.e. bind
the BindingSource to a list of custom objects and then bind the
DataGridView to the BindingSource, you could call the ResetBindings method
of the BindingSource to cause the DataGridView to reread all the items in
the list and refresh their displayed values. The following is a sample.

List<Person> lists = new List<Person>();
// add some objects in the lists
....
BindingSource bs = new BindingSource(lists, "");
dataGridView1.DataSource = bs;
lists.Add(new Person("6","cc"));
bs.ResetBindings(false);
I need the AutoGenerateColumns set to generate Columns because I want to
specify specific attributes such as width, etc at design time.

You have a misunderstanding here. The AutoGenerateColumns property is used
to indicate whether columns are created automatically when the DataSource
or DataMember properties are set at run time. We can only set the
AutoGenerateColumns property in code, because this property is not
available in the Properties window.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Linda
I chose to use a bindingsource at design time. The DataGridView will bind
properly once when the program begins. bs.ResetBindings(false) will NOT
cause the grid to rebind. I had already tried that. I have tried every
possible method of the bindingsource and the grid and nothing will rebind
the grid to show new data. the only thing that will rebind the grid is to
end the program and start it over.

Since I have to move on decided to just put up a blank DataGridView and set
it's datasource to a datatable at run time. That works just fine.

It looks to me that Forms BindingSources are in that same category as
ASP.Net .xsds: Not really usefull.

But, thanks for your help on this
--
Regards,
Gary Blakely

Linda Liu said:
Hi Gary,

Thank you for your response.

Could you tell me what kind of data source you're using? I guess it is a
list of custom objects, right?

Only those data source that implements the IBindingList interface can
support change notification for when the list itself changes(for example,
the number of items in the list increases or decreases). BindingList<T>
provides a generic implementation of the IBindingList interface, so it is
a
good candidate to use as the data source.

Note that a class must implement the INotifyPropertyChanged interface to
make change notification when any of its property values change.

To summary, I suggest that you use the BindingList<T> as the data source
and implement the INotifyPropertyChanged interface for your business
class.
I will illuminate this with a sample. It requires that you add a
DataGridView and two Buttons on the form.

public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
BindingList<Person> lists = new BindingList<Person>();
lists.Add(new Person("1", "aa"));
lists.Add(new Person("2", "bb"));
this.dataGridView1.DataSource = lists;
}
private void button1_Click(object sender, EventArgs e)
{
BindingList<Person> lists = this.dataGridView1.DataSource as
BindingList<Person>;
lists.Add(new Person("3", "22"));
}
private void button2_Click(object sender, EventArgs e)
{
BindingList<Person> lists = this.dataGridView1.DataSource as
BindingList<Person>;
lists[1].ID = "5";
}
class Person:INotifyPropertyChanged
{
string id;
string name;
public string ID
{
get { return id; }
set
{
id = value;
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs("ID"));
}
}
public string Name
{
get { return name; }
set
{
name = value;
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs("Name"));
}
}
public Person(string _id,string _name)
{
id = _id;
name = _name;
}
public event PropertyChangedEventHandler PropertyChanged;

}
}

Run the program. When you click the button1, you should see a new row is
added in the DataGridView. When you click the button2, you should see the
value of column 'ID' in the second row is changed to 5.

For more information on interfaces related to data binding, you may read
the following MSDN document:
'Interfaces Related to Data Binding '
http://msdn2.microsoft.com/en-us/library/41e17s4b.aspx

BTW, the Refresh or Invalidate method is used to cause the DataGridView to
be redrawn. It's no use calling either of these two methods to get the
DataGridView to show new data in the data source.

Alternatively, if you're using BindingSource as the data source, i.e. bind
the BindingSource to a list of custom objects and then bind the
DataGridView to the BindingSource, you could call the ResetBindings method
of the BindingSource to cause the DataGridView to reread all the items in
the list and refresh their displayed values. The following is a sample.

List<Person> lists = new List<Person>();
// add some objects in the lists
....
BindingSource bs = new BindingSource(lists, "");
dataGridView1.DataSource = bs;
lists.Add(new Person("6","cc"));
bs.ResetBindings(false);
I need the AutoGenerateColumns set to generate Columns because I want to
specify specific attributes such as width, etc at design time.

You have a misunderstanding here. The AutoGenerateColumns property is used
to indicate whether columns are created automatically when the DataSource
or DataMember properties are set at run time. We can only set the
AutoGenerateColumns property in code, because this property is not
available in the Properties window.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
It looks to me that Forms BindingSources are in that same category as
ASP.Net .xsds: Not really usefull.
I find comments like that rather pathetic. Binding source works really
well in many circumstances. Just because it's not suited to your
particular requirement doesn't make it "not really useful", it simply
makes it not useful for your situation.
 
Back
Top