Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml as datasource

Status
Not open for further replies.

regulardude

Programmer
Oct 24, 2007
18
US
Hi,

I am getting to where I need to be, but slow going, and thanks for all of the previous help.

What I have so far...

Form loads and checks to see if xml file exists, if it does, it loads into runtime dataset and binds to datagrid:
Code:
private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //File.OpenRead("C:\\DriveData.xml");
                DataSet ds1 = new DataSet();
                ds1.ReadXml("C:\\DriveData.xml");
                dataGridView1.DataSource = ds1;
                dataGridView1.DataMember = "DriveInfo";

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Console.WriteLine("Use the Open File button.");
            }
        }


On the form, if the datagrid is not populated, the user can load an excel file:
Code:
 private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Excel (*.xls)|*.xls";
            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;


            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                using (System.Data.OleDb.OleDbConnection objCon = new System.Data.OleDb.OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";",
            openFileDialog1.FileName)))
                using (System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand("Select * From [sheet1$]", objCon))
                using (System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(cm))
                {
                   DataSet ds1 = new DataSet();
                    objCon.Open();
                    da.Fill(ds1,"DriveInfo");
                    objCon.Close();
                    ds1.WriteXml("C:\\DriveData.xml",XmlWriteMode.WriteSchema);
                    ds1.ReadXml("C:\\DriveData.xml");
                    dataGridView1.DataSource = ds1;
                    dataGridView1.DataMember = "DriveInfo";


                }
            }
        }

This works great, but the part that I thought would be easier is harder for me at least.

I have a strong typed dataset created in my vs project with one datatable.

This dataset is part of the same initial form that loads and is binded to textbox, and other type controls.

I also have the binding source and binding navigator components at the bottom of the design screen for this dataset.

Here is what I want to do...

I want to write the strong typed dataset to an xml file, just like the weak typed one before, and then reload the xml file back to the dataset, maybe this step is not neccessary until I have changed data.

Anyway, I want this dataset editable/updated as if it were connecected to a dataadapter that was connected to a database.

My Save Button is greyed out on the binding navigator.

When I save changes, I want to have them save to the xml file, so that even if the application closes, the changes will be there.

Thanks, sorry for long winded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top