If you are storing data locally use a data set. Drag and drop a dataset to the winform, then select untyped. If you already have data inside the gridview you can easily add that data by :
DataSet DataSet1 = new dataset
dataset1.Tables.Add("Main");
foreach(DataGridViewColumn col in GridView.Columns)
{
string name = col.Name.ToString();
DataSet1.Tables["Main"].Columns.Add(name);
DataSet1.AcceptChanges();
}
foreach(DataGridViewRow row in GridView.Rows)
{
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
DataSet1.Tables["Main"].Rows.Add(value1,value2);
DataSet1.AcceptChanges();
}
To save the dataset use this code:
string path = "path to where to save";
DataSet1.WriteXml(path,XmlWriteOption.WriteScheme);
to read dataset into program
string path = "path of saved xml";
Dataset1.ReadXml(path);
//Binds with DataGrid
DataGridView1.DataSource = DataSet1;
DataGridView1.DataMember = "Main";
DataGridView1.Update();
in order to for adding a new row manually you will need to make a RowsAdded Event if your using visual studio in properties click the lighting bolt and scroll down to RowsAdded and double click it.
DataGridView1_RowsAdded_Event(object sender, EventArgs e)
{
DataSet1.AcceptChanges();
}
you will do the same with Rows Deleted. By calling DataSet1.AcceptChanges()
you are telling the program to keep current changes.
ToBindingList()
will loose the add/delete functionality? In factToBindingList
is specifically provided to be used for WF data binding. – Ivan StoevbsTripStops.DataSource = ctxTripStops.TripStops.Local.ToBindingList();
should do. The returnedBindingList<T>
implementation will stay in sync withDbSet<T>.Local
property. – Ivan Stoev