Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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>



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 :
C#
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!!!
Posted
Updated 5-Jun-14 1:40am
v2
Comments
ZurdoDev 5-Jun-14 7:31am    
Please click on Improve question and show us the code you are using to currently save to xml.

1 solution

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.
C#
private DataSet dsIsland = new DataSet("DataSetIsland");


2. In FormLoad create a DataTable and add the columns you want.
C#
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. Set default values to some columns
C#
dcSelect.DefaultValue = false;

What other columns need default values?

4. Connct the DataSet to the DataGridView
C#
dgvIsland.DataSource = dsIsland;
dgvIsland.DataMember = dt.TableName;



Inside your existing method
C#
private void createXML(string filename)
{
  dsIsland.AcceptChanges();
  dsIsland.WriteXml(filename);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900