Reputation: 9
I have a DataGridView and multiple objects containing data whit which I want to populate the DataGridView every time I switch between selecting one of those objects. I also want to update the data in those objects every time I edit the DataGridView.
The DataGridView already has predefined columns, some of them being a ComboBox column.
Say I have
public class OB()
{
public DataTable data { get; private set; } = new DataTable();
}
obj1 = new OB();
obj2 = new OB();
.
.
.
public void some_selection_event(object s, EventArgs e)
{
dataGridView1.DataSource = ((OB)s).data;
}
If I edit dataGridView1
while obj1.data
is bound to its DataSource
and then bind something else to it before binding obj1.data
back to it again, dataGridView1
show that it only remembers the number of rows it had whilst I was editing it with obj1.data
bound to its DataSource
, and the data in the cells is lost, every cell is empty.
Is there a way that I can easily map a DataSource to a DataGridView and have then automatically update, or am I missing something here?
Upvotes: 0
Views: 140
Reputation: 9
If you have predefined Columns
in your DataGridView
, use DataGridView.DataPropertyName
to make sure that the DataSource
and DataGridView
are both addressing the same data.
Upvotes: 0