0 Replies - 533 Views - Last Post: 23 May 2012 - 01:13 PM Rate Topic: -----

#1 intentarr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 11

Can I save changed information in datagridview at runtime?

Posted 23 May 2012 - 01:13 PM

Hello,
I have just started to learn C# and I'm a little bit confused about DataSet/DataTable/DataGridView.
For now I'm having a simple form where it is DataGridView, listBox and insert delete buttons. When my form loads in listBox I'm getting excel sheet's list. Then I choose one of the sheets and then it appears on DataGridView (all information I'm writing to DataSet and DataTable). And I would like to add or delete columns at runtime. Here is my code for delete button:
excelConn = new OleDbConnection();
            excelConn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + excelFilePath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
            DataTable dt = new DataTable();
            OleDbDataAdapter ExcelAdapt = new OleDbDataAdapter("Select * From " + "[" + tableName + "]", excelConn);
            ExcelAdapt.Fill(dt);
            try
            {
                dt.Columns.Remove(colName);
                dt.AcceptChanges();
                var bds = new BindingSource();
                bds.DataSource = dt;
                grid.DataSource = bds;

                MessageBox.Show("Column(s) deleted ");
                excelConn.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



The problem I faced is that I can't save changes made at runtime. And even at runtime these changes are temporary. If at runtime I add new column and then I choose another sheet from listBox these changes disappear.
I find this tutorial (http://www.dreamincode.net/forums/topic/238727-insert-update-and-delete-records-in-table-with-datagridview-using-c%23/) but if I understood well it changes database, well in my case it would change excel file (but I want to have original excel file and making changes only in DataSet and DataTable).

Can I have make changes only in DataSet/DataTable (if yes then how?) ?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1