c#中怎么把datagridview里的数据写入xml文件中
1个回答
展开全部
简单的帮搂主搞了下伪代码,可以作为参考:
private void SaveToXml()
{
this.CreateColumnXmlNode();
for (int index = 0; index < this.dataGridView1.Rows.Count; index++)
{
DataGridViewRow row = this.dataGridView1.Rows[index];
this.CreateXmlNode(row);
}
}
private void CreateColumnXmlNode()
{
for (int index = 0; index < this.dataGridView1.Columns.Count; index++)
{
string szHeaderText = this.dataGridView1.Columns[index].HeaderText;
//
//.....保存列标题和索引到xml节点的对应属性中
}
}
private void CreateRowXmlNode(DataGridViewRow row)
{
for (int index = 0; index < row.Cells.Count; index++)
{
DataGridViewCell cell = row.Cells[index];
//
//.....保存单元格内容,以及单元格索引到xml节点的对应属性中
}
}
xml结构:
<DataGridViewData>
<Columns>
<Item Index = "索引" Text = "列标题"/>
......
</Columns>
<Rows>
<Item Index = "索引" Value = "内容"/>
......
</Rows>
</DataGridViewData>
参考:
http://bbs.csdn.net/topics/290019619
private void SaveToXml()
{
this.CreateColumnXmlNode();
for (int index = 0; index < this.dataGridView1.Rows.Count; index++)
{
DataGridViewRow row = this.dataGridView1.Rows[index];
this.CreateXmlNode(row);
}
}
private void CreateColumnXmlNode()
{
for (int index = 0; index < this.dataGridView1.Columns.Count; index++)
{
string szHeaderText = this.dataGridView1.Columns[index].HeaderText;
//
//.....保存列标题和索引到xml节点的对应属性中
}
}
private void CreateRowXmlNode(DataGridViewRow row)
{
for (int index = 0; index < row.Cells.Count; index++)
{
DataGridViewCell cell = row.Cells[index];
//
//.....保存单元格内容,以及单元格索引到xml节点的对应属性中
}
}
xml结构:
<DataGridViewData>
<Columns>
<Item Index = "索引" Text = "列标题"/>
......
</Columns>
<Rows>
<Item Index = "索引" Value = "内容"/>
......
</Rows>
</DataGridViewData>
参考:
http://bbs.csdn.net/topics/290019619
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询