2012-10-12 40 views
0

如何添加DataGridView中的数据,数据集(包括列名)添加DataGridView中的数据为DataSet

嗯,我开始并在下面的代码的中间stucked起来:

   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 

然后,我需要该数据集写入到一个XML文件,这样

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

请提前帮助
感谢。

回答

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(); 
+0

感谢您的答复。然后如何将它添加到带有两个额外标签的xml文件中,我提到了这个问题。我知道如何写,但我不知道如何添加额外的标签。 –

+0

@Mr_Green chk更新后的部分 – Talha

+0

实际上,我不需要为创建的表格添加两个父元素,即“stock”和“items”。正如我在问题中提到的那样 –

相关问题