Reputation: 21
I have a dataGridView, which should use a list as a dataSource. When running the application, it shows the grid fine with columns (auto-generated). When a new Person-object is created and added to the list called Persons, it just add an empty row. When adding new persons after this, no changes is made.
The system is using a property in MainForm called Project, where it stores it's list used for the grid.
public partial class MainForm : Form
{
public static DataProject Project { get; set; }
public MainForm()
{
InitializeComponent();
//show a dialog, creating the Project-object
BindingSource bs = new BindingSource();
bs.DataSource = Project.Persons;
dataGridView1.DataSource = bs;
}
private void buttonAddPerson_Click(object sender, EventArgs e)
{
//show a dialog, creating a person and adding him to Project.Person (list).
dataGridView1.Refresh();
dataGridView1.Update();
}
}
Here is the DataProject-object:
public class DataProject
{
public string Name { get; set; }
public List<Person> Persons { get; set; }
public DataProject()
{
Persons = new List<Person>();
}
}
Upvotes: 2
Views: 126
Reputation: 19
Create a global variable for BindingSource
BindingSource bs = new BindingSource();
and on button click just reset this bindingdource
bs.ResetBindings(false);
Upvotes: 1
Reputation: 175
dataGridView1.DataBind(); add this at the end. I had the same issue once, it worked for me!
public MainForm()
{
InitializeComponent();
//show a dialog, creating the Project-object
BindingSource bs = new BindingSource();
bs.DataSource = Project.Persons;
dataGridView1.DataSource = bs;
dataGridView1.DataBind();
}
Upvotes: 0