Reputation: 1157
I've used one module to read excel files and fill the data from them into a DataTable. This DataTable gets then displayed into a DataGridView form.
With that I have no problems, but the thing is that I want to catch any changes made to that DataTable while it was displayed in the GridView and to save them.
In other words, I want to call a function each time something has been changed on the updated new DataTable (I have no problems, if this would require an additional button (e.g. "Save Changes") as well).
I've read about binding source and adapters, but I am quite confused and cannot get them to work properly. So any help would be appreciated, here's the code.
public partial class MainForm : Form
{
DataTable dTable = new DataTable();
BindingSource bSource = new BindingSource();
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void Button1Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "d:\\" ;
openFileDialog1.Filter = /*txt files (*.txt)|*.txt|xcel files (*.xcel*)|*.xcel*|*/ "All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true ;
openFileDialog1.Title = "Choose a file to read";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
string szChosenFileNameDirectory = openFileDialog1.FileName;
string szChosenFileExtension = szChosenFileNameDirectory.Substring(szChosenFileNameDirectory.LastIndexOf("."), szChosenFileNameDirectory.Length - szChosenFileNameDirectory.LastIndexOf("."));
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
ExcelFile ef = ExcelFile.Load(szChosenFileNameDirectory);
foreach (ExcelWorksheet sheet in ef.Worksheets)
{
foreach(ExcelColumn column in sheet.Columns)
{
if(column.Index == sheet.CalculateMaxUsedColumns())
break;
DataColumn col = new DataColumn();
col.ColumnName = column.Name;
dTable.Columns.Add(col);
}
foreach (ExcelRow row in sheet.Rows)
{
DataRow r = dTable.NewRow();
r.BeginEdit();
foreach (ExcelCell cell in row.AllocatedCells)
{
r[cell.Column.Name] = cell.Value;
}
r.EndEdit();
dTable.Rows.Add(r);
}
}
//setting the datasource, ok
dataGridView1.DataSource = dTable;
//Handlig a changed row or?
dTable.RowChanged += new DataRowChangeEventHandler(Row_Changed);
}
}
public static void Row_Changed(object sender, DataRowChangeEventArgs e)
{
}
}
P.S.: You should have this :/
using GemBox.Spreadsheet;
Upvotes: 0
Views: 1510
Reputation: 623
There are several ways to do this and several topics that regards this issue.
A quick fix is rebinding your dataSource to the dataGridView via a method attached to a 'refresh' button.
Use the following method, you can assign it to a on_click button event that you must create beforehand:
function bool refresh()
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = dTable;
return true;
}
Alternatively, try looking around the forum, there are several similar topics that show different methods:
Upvotes: 1