简体   繁体   中英

Add data of DataGridView to DataSet

How can i add data of DataGridView to DataSet (including column names)

well i started and stucked up in middle of the below code:

             DataTable table = new DataTable();
             DataRow newRow = new DataRow();
             table.Columns.Add("productname");  //first column
             table.Columns.Add("brandname");    //second column
             table.Columns.Add("quantity");     //third column
             table.Columns.Add("price");        //fourth column

Then I need to write this DataSet to a XML file like this

<stock>
    <items>   //How to add these two extra tags? ('stock' and 'items')
       ----Column Names----
    <items>
<stock>

Please Help
Thanks in Advance.

DataSet ds = new DataSet("stock");
DataTable table = new DataTable("items");
table.Columns.Add("productname");  //first column
table.Columns.Add("brandname");    //second column
table.Columns.Add("quantity");     //third column
table.Columns.Add("price");        //fourth column

Name your dataset and datatable as described above. It will write your xml as you need.

 DataRow newRow = table.NewRow();
newRow["productname"] = "some value";
newRow["brandname"] = "some value";
newRow["quantity"] = "some value";
newRow["price"] = "some value";
table.Rows.Add(newRow);
ds.Tables.Add(table);

Edited:

string xml = ds.GetXml();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM