1

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));

        }
    }

}
nordscan
  • 137
  • 1
  • 11
  • 2
    This is called *serialization*. You already choose xml, but there are more options, see [this](http://stackoverflow.com/q/6115721/1997232). – Sinatr Feb 01 '17 at 12:48
  • You can, how you do that though depends on how those textboxes are related to the datagrid data. How are you currently saving the data in your textboxes? – gmiley Feb 01 '17 at 12:50
  • Thank you for introducing of serialization. Found good example [link](http://csharphelper.com/blog/2016/01/serialize-and-deserialize-objects-in-c/) – nordscan Feb 01 '17 at 14:13

1 Answers1

0

Finaly i use save textboxes and datagridview through dataset.WriteXml

//XML save throught dataset
    private void XMLsave(object sender, EventArgs e)
    {
        dataGridView1.AllowUserToAddRows = false;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.TableName = "OrderData";
        dt.Columns.Add("OrderNr");
        dt.Columns.Add("Custommer");
        dt.Columns.Add("Material");
        dt.Columns.Add("MaterialCode");
        ds.Tables.Add(dt);

        DataTable dt1 = new DataTable();
        dt1.TableName = "Data";
        dt1.Columns.Add("Lenght");
        dt1.Columns.Add("Width");
        dt1.Columns.Add("Qty");
        ds.Tables.Add(dt1);

        DataRow row = ds.Tables["OrderData"].NewRow();
        row["OrderNr"] = tbOrderNr.Text;
        row["Custommer"] = tbCustommer.Text;
        row["Material"] = tbMaterial.Text;
        row["MaterialCode"] = tbForm2MatCode.Text;
        ds.Tables["Data"].Rows.Add(row);

        foreach (DataGridViewRow r in dataGridView1.Rows)
        {
            DataRow row1 = ds.Tables["Data"].NewRow();
            row1["Lenght"] = r.Cells[0].Value;
            row1["Width"] = r.Cells[1].Value;
            row1["Qty"] = r.Cells[2].Value;
            ds.Tables["Data"].Rows.Add(row1);
        }
        ds.WriteXml("test.xml");
        dataGridView1.AllowUserToAddRows = true;

    }

and then load it with:

private void XmlLoad(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml("test.xml");
        tbOrderNr.Text = ds.Tables["OrderData"].Rows[0][0].ToString();
        tbCustommer.Text = ds.Tables["OrderData"].Rows[0][1].ToString();
        tbMaterial.Text = ds.Tables["OrderData"].Rows[0][2].ToString();
        tbForm2MatCode.Text = ds.Tables["OrderData"].Rows[0][3].ToString();

        foreach (DataRow item in ds.Tables["Data"].Rows)
        {
            dataGridView1.AllowUserToAddRows = false;
            int n = dataGridView1.Rows.Add();
            dataGridView1.Rows[n].Cells[0].Value = tem["Lenght"].ToString();
            dataGridView1.Rows[n].Cells[1].Value = item["Width"].ToString();
            dataGridView1.Rows[n].Cells[2].Value = item["Qty"].ToString();
            dataGridView1.AllowUserToAddRows = true;
        }
nordscan
  • 137
  • 1
  • 11