I have a form with 4textboxes + datagridview with 11columns.
Have from here a method to save datagridview this work fine, but is it possible to improve it so i can save not only the datagridview values, but the textboxes too??(then load too.)
thank you
Or is there something others that can be used for this?
private DataTable GetDataTableFromDGV(DataGridView dataGridView1)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
if (column.Visible)
{
dt.Columns.Add();
}
}
object[] cellValues = new object[dataGridView1.Columns.Count];
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
cellValues[i] = row.Cells[i].Value;
}
dt.Rows.Add(cellValues);
}
return dt;
}
private void SaveXML(object sender, EventArgs e)
{
//Savefile dialog for save CSV file
SaveFileDialog savefile = new SaveFileDialog();
savefile.FileName = tbOrderNr.Text + " - " + tbCustommer.Text + ".xml";
savefile.Filter = "xml files (*.xml)|*.xml";
if (savefile.ShowDialog() == DialogResult.OK)
{
DataTable dT = GetDataTableFromDGV(dataGridView1);
DataSet dS = new DataSet();
dS.Tables.Add(dT);
dS.WriteXml(File.OpenWrite(savefile.FileName));
}
}
}