如何将DataGridView保存到DataGridView中具有相同标题列的xmlfile中? [英] How to save a DataGridView in to a xmlfile with same header's column in DataGridView?

查看:63
本文介绍了如何将DataGridView保存到DataGridView中具有相同标题列的xmlfile中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将DataGridView保存到DataGridView中具有相同标题列的xmlfile?

例如,我在DataGridView中的标题列是:c1,c2,c3,c4

和我的xmlfile必须遵循以下结构,我该如何创建它?



 <  数据集 >  
< r >
< C1 > v1 < / C1 >
< C2 > v2 < / C2 >
< C3 < span class =code-keyword>> v3 < / C3 >
< C4 < span class =code-keyword>> v4 < / C4 >
< / r >
< r >
< C1 > t1 < / C1 >
< C2 < span class =code-keyword>> t2 < / C2 >
< C3 < span class =code-keyword>> t3 < / C3 >
< C4 < span class =code-keyword>> t4 < / C4 >
< / r >
< / DataSet <跨度class =code-keyword>>

解决方案

选项很少。其中一个是使用 XmlDocument [ ^ ]和 XmlElement [ ^ ] class。



 XmlDocument xdoc =  new  XmlDocument (); 
XmlElement xroot = new XmlElement( DataSet );
XmlElement xele;

foreach (DataGridViewRow dgr in DataGridView1.Rows)
{
xele = new XmlElement( r );
foreach (DataGridViewColumn dgc in DataGridView1.Columns)
{
xele.Add( new XmlElement(dgc.Name,dgr.Cells [dgc.Index] .Value);
}
xroot.Add( xele);
}
xdoc.Add(xroot);





如需了解更多信息,请参阅:

DataGridViewRow [ ^ ]

DataGridView.Rows [ ^ ]

DataGridViewColumn [ ^ ]

DataGridView.Columns [ ^ ]


Hi,How to save a DataGridView in to a xmlfile with same header's column in DataGridView?
for example, my header's column in DataGridView are:c1,c2,c3,c4
and my xmlfile must be following structure,How can I create it?

<Dataset>
  <r>
    <C1>v1</C1>
    <C2>v2</C2>
    <C3>v3</C3>
    <C4>v4</C4>
  </r>
  <r>
    <C1>t1</C1>
    <C2>t2</C2>
    <C3>t3</C3>
    <C4>t4</C4>
  </r>
</DataSet>

解决方案

There's few options. One of them is to use XmlDocument[^] and XmlElement[^] class.

XmlDocument xdoc = new XmlDocument();
XmlElement xroot = new XmlElement("DataSet");
XmlElement xele;

foreach(DataGridViewRow dgr in DataGridView1.Rows)
{
    xele = new XmlElement("r");
    foreach(DataGridViewColumn dgc in DataGridView1.Columns)
    {
        xele.Add(new XmlElement(dgc.Name, dgr.Cells[dgc.Index].Value);
    }
    xroot.Add(xele);
}
xdoc.Add(xroot);



For further information, please see:
DataGridViewRow[^]
DataGridView.Rows[^]
DataGridViewColumn[^]
DataGridView.Columns[^]


这篇关于如何将DataGridView保存到DataGridView中具有相同标题列的xmlfile中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆