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 there is DataGridView
, listBox
, 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
). 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);
}
我面临的问题是,我无法保存运行时甚至运行时所做的更改。 这些更改是临时性的。 如果运行时我添加了新的列, 然后从 ListBox
中选择另一个工作表, 这些更改就会消失 。
我DataSet
和DataTable
作出修改)。
我只能更改 DataSet/DataTable
。 如果有,如何更改?