Hi all, I am trying to implement read and write functionality between DataGridViewload controls and an XML file. The code to write the the XML file is as follows
DataTable dt_csk = new DataTable("CONTROL_KEYS");
dt_csk.Columns.Add("key", typeof(System.String));
dt_csk.Columns.Add("value", typeof(System.String));
for (idx = 0; idx < dgv_csk.Rows.Count; idx++)
{
dt_csk.Rows.Add(dgv_csk[0, idx].Value.ToString(), dgv_csk[1, idx].Value.ToString());
}
dt_csk.EndLoadData();
dt_csk.AcceptChanges();
dgv_csk.DataSource = dt_csk.DefaultView;
I then add all DataTables to a DataSet like so
DataSet ds = new DataSet();
ds.Tables.Add(dt_csk);
ds.Tables.Add(dt_cdk);
ds.Tables.Add(dt_cak);
/* Output the contents of the DataSet as an XML file */
ds.WriteXml(logfile, XmlWriteMode.IgnoreSchema);
I read the XML into the DataGridView elements as follows:
XmlDataDocument xmldata = new XmlDataDocument();
xmldata.DataSet.ReadXml(xml_filename);
dgv_csk.DataSource = xmldata.DataSet;
dgv_csk.DataMember = "CONTROL_STATIC_KEYS";
This works great but there is one problem. PROBLEM When I import info from the XML file, there is one additional row that is blank in each DataGridView control. I have some other functions where I use the number of rows in the DGV as an index, and this additional row is screwing things up. Could you tell me what I am doing wrong? Thanks! N.
modified on Monday, August 29, 2011 1:57 AM