I have a datagridview that is not bound to a table in a database. The datagridview is being populated by the contents of a drop down list and a text box on a button click. I wan
You have many options. Here is a simple solution that uses XML serialization.
Note that it makes a few assumptions:
To save the other data types you should create a serializable structure!
private void saveButton_Click(object sender, EventArgs e)
{
List> data = new List>();
foreach(DataGridViewRow row in dgInsertedInfo.Rows)
{
List rowData = new List();
foreach (DataGridViewCell cell in row.Cells)
rowData.Add(cell.FormattedValue.ToString());
data.Add(rowData);
}
XmlSerializer xs = new XmlSerializer(data.GetType());
using (TextWriter tw = new StreamWriter(yourFileName))
{
xs.Serialize(tw, data);
tw.Close();
}
}
private void loadButton_Click(object sender, EventArgs e)
{
List> data = new List>();
XmlSerializer xs = new XmlSerializer(data.GetType());
using (TextReader tr = new StreamReader(yourFileName))
data = (List>) xs.Deserialize(tr);
foreach (List rowData in data)
dgInsertedInfo.Rows.Add(rowData.ToArray());
}