如何将datagridview的空行列值存储到xml文件中? [英] How to store even empty row column values of a datagridview into a xml file?

查看:146
本文介绍了如何将datagridview的空行列值存储到xml文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个datagridview,其中有一个checkBoxColumn和3个textColumns。

我想把它的内容存储到一个xml文件中,其中if未选中复选框列,它将false存储为true。此外,如果某个字段为空,则应该不存储任何内容。



但是发生的情况是,如果没有选中复选框,则不会提及相同的内容。类似地,如果字段中没有条目,则将其省略。



Hi,
I have a datagridview in which there is a checkBoxColumn and 3 textColumns.
I want to store the contents of the same into an xml file where if the checkbox column is not selected, it stores false else true. Also if a certain field is empty it should store nothing.

But what is happening is , in case the checkbox is not selected there is no mention of the same . Similarly if there is no entry in a filed, it is omitted.

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table1>
    <Select>True</Select>
    <Island>AppReplayEth.zip</Island>
    <PortConfig>d:\abc.txt</PortConfig>
    <Setup_x0020_File>d:\pics</Setup_x0020_File>
    <Time_x0020_Out>1</Time_x0020_Out>
  </Table1>
  <Table1>
    <Island>AppReplayIp.zip</Island>
    <Settings_x0020_File>AMIT</Settings_x0020_File>
    <Time_x0020_Out>0</Time_x0020_Out>
  </Table1>
</NewDataSet>







在这里你看到Bold中的那些存在于一个表中但在另一个表中不存在因为值是空的。如果任何字段为空,我该怎么办?



< portconfig>

< setup file =>

< Select> False< / Select> //如果没有选中复选框



以下是我用来保存到xml文件的代码:




Here you see the ones in Bold are present in one table but absent in the other because the values were empty. How can i do the following if any field is empty

<portconfig>
<setup file="">
<Select>False</Select> // if the checkbox is not selected

The following is the code i am using to save to xml file :

private void createXML(string filename)
        {
            DataTable dt = new DataTable();
            foreach (DataGridViewColumn col in dgvIsland.Columns)
            {
                dt.Columns.Add(col.HeaderText);
            }
            foreach (DataGridViewRow row in dgvIsland.Rows)
            {
                DataRow dRow = dt.NewRow();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dRow[cell.ColumnIndex] = cell.Value;
                }
                dt.Rows.Add(dRow);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.WriteXml(filename);
        }





请帮助!!!



Please help!!!

推荐答案

我希望这会有所帮助。

更好的方法是使用 DataSet 作为 DataGridView



1.创建一个DataSet作为你班级的私人成员。

I hope this helps.
A better approach is to use the DataSet as a data source for the DataGridView.

1. Create a DataSet as a private member of your class.
private DataSet dsIsland = new DataSet("DataSetIsland");





2.在FormLoad中创建 DataTable 并添加你想要的列。



2. In FormLoad create a DataTable and add the columns you want.

DataTable dt = dsIsland.Tables.Add("Island");

DataColumn dcSelect = dt.Columns.Add("Select", typeof(bool));
DataColumn dcIsland = dt.Columns.Add("Island", typeof(string));
DataColumn dcPortConfig = dt.Columns.Add("PortConfig", typeof(string));
DataColumn dcSetupFile = dt.Columns.Add("SetupFile", typeof(string));
DataColumn dcSettingsFile = dt.Columns.Add("SettingsFile", typeof(string));





3.将默认值设置为某些列



3. Set default values to some columns

dcSelect.DefaultValue = false;



其他哪些列需要默认值?



4.连接 DataSet DataGridView


What other columns need default values?

4. Connct the DataSet to the DataGridView

dgvIsland.DataSource = dsIsland;
dgvIsland.DataMember = dt.TableName;







在现有方法内




Inside your existing method

private void createXML(string filename)
{
  dsIsland.AcceptChanges();
  dsIsland.WriteXml(filename);
}


这篇关于如何将datagridview的空行列值存储到xml文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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