Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Updating bound DataGridView?
 

Updating bound DataGridView?

I have a datagridview that is being bound via the following code:

            companies = new BindingList<Company>(PersistenceManager.Instance.RetrieveAll<Company>(SessionAction.BeginAndEnd));
            companies.AllowNew = true;
            companies.AllowEdit = true;

            bindingSource.DataSource = companies;

            potentialInvestorDataGridView.AutoGenerateColumns = false;
            potentialInvestorDataGridView.DataBindings.Add("DataSource", bindingSource, "PotentialInvestors");


I am then adding to the PotentialInvestors (which is an IList<Investor>) with the following code:

            Company company = bindingSource.Current as Company;
            // Create our investor for use
            company.PotentialInvestors.Add(new Investor { CompanyId = company.Id });


However the datagridview does not get updated with a new row. What do I need to do to refresh it properly?
zenox  Wednesday, September 16, 2009 5:02 PM
Change the base type ofPotentialInvestors to BindingList<Investor>
ImplementINotifyPropertyChangedin theInvestor class.


The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP
Sheng Jiang 蒋晟  Wednesday, September 16, 2009 8:41 PM
Hi zenox,

>potentialInvestorDataGridView.DataBindings.Add("DataSource", bindingSource, "PotentialInvestors");

It is not clear to me why you use simple binding for a list and DataGridView? What you need to do is using complex binding. Please look at the following code.
public partial class Form1 : Form
{
private BindingList<Company> compList = new BindingList<Company>();

public Form1()
{
InitializeComponent();
compList.Add(new Company(1, "Microsoft"));
compList.Add(new Company(2, "Google"));

dataGridView1.DataSource = compList;
}

private void button1_Click(object sender, EventArgs e)
{
compList.Add(new Company(3, "IBM"));
}
}

public struct Company
{
private int companyID;
public int CompanyID
{
get { return companyID; }
set { companyID = value; }
}

private string companyName;
public string CompanyName
{
get { return companyName; }
set { companyName = value; }
}

public Company(int id, string name)
{
this.companyID = id;
this.companyName = name;
}
}
First I need to create a type (company type) with CompanyID and CompanyName. Then I call BindingList.Add method to add new items. You can see I set the DataSource property of DataGridView to bind it to compList.

When I click button1, I added another item into the compList, it displayed on the DataGridView immediately.

Hope this helps you. If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 21, 2009 7:37 AM
Change the base type ofPotentialInvestors to BindingList<Investor>
ImplementINotifyPropertyChangedin theInvestor class.


The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP
Sheng Jiang 蒋晟  Wednesday, September 16, 2009 8:41 PM
Hi zenox,

>potentialInvestorDataGridView.DataBindings.Add("DataSource", bindingSource, "PotentialInvestors");

It is not clear to me why you use simple binding for a list and DataGridView? What you need to do is using complex binding. Please look at the following code.
public partial class Form1 : Form
{
private BindingList<Company> compList = new BindingList<Company>();

public Form1()
{
InitializeComponent();
compList.Add(new Company(1, "Microsoft"));
compList.Add(new Company(2, "Google"));

dataGridView1.DataSource = compList;
}

private void button1_Click(object sender, EventArgs e)
{
compList.Add(new Company(3, "IBM"));
}
}

public struct Company
{
private int companyID;
public int CompanyID
{
get { return companyID; }
set { companyID = value; }
}

private string companyName;
public string CompanyName
{
get { return companyName; }
set { companyName = value; }
}

public Company(int id, string name)
{
this.companyID = id;
this.companyName = name;
}
}
First I need to create a type (company type) with CompanyID and CompanyName. Then I call BindingList.Add method to add new items. You can see I set the DataSource property of DataGridView to bind it to compList.

When I click button1, I added another item into the compList, it displayed on the DataGridView immediately.

Hope this helps you. If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 21, 2009 7:37 AM

You can use google to search for other answers

Custom Search

More Threads

• ComboBox & DataBindings Problem
• Change colour of modified datagrid row
• Updated records in a DataTable don't apply to the DB (MSDN Example)
• Inputbox - Datetimepicker
• Report Viewer DataBinding ?
• DataBinding CHECKEDListBox, how to make system autocheck items in the chkList as it populates items in it???
• Questions about MSDE...
• SOS!!!! Urgent, datagrid column style
• Master-Detail Problem: Advanced SQL Generations Options has no "Generate INSERT, UPDATE, and DELETE statements" highlighted
• Combine datatable common rows into single row with comma seperated cell