Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML File -> DataGridView - One additional row of data

XML File -> DataGridView - One additional row of data

Scheduled Pinned Locked Moved C#
databasexmlhelpquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheBlindWatchmaker
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups