0

如何将 DataGridView 的数据添加到 DataSet(包括列名)

好吧,我开始并坚持在下面的代码中间:

             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

然后我需要将此 DataSet 写入这样的 XML 文件

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

请帮助
提前致谢。

4

1 回答 1

0
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

如上所述命名您的数据集和数据表。它会根据需要编写您的 xml。

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

编辑:

string xml = ds.GetXml();
于 2012-10-12T05:05:02.143 回答