I want to save any changes to a DataTable in a DataGridView

时间:2016-04-07 10:33:30

标签: c# datagridview datatable

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;

1 个答案:

答案 0 :(得分:1)

有几种方法可以解决此问题。

快速修复是通过附加到'刷新'的方法将dataSource重新绑定到dataGridView。按钮。

使用以下方法,您可以将其分配给您必须事先创建的on_click按钮事件:

function bool refresh()
{
    dataGridView1.DataSource = null;
    dataGridView1.DataSource = dTable;

    return true;
}

或者,尝试浏览论坛,有几个类似的主题显示不同的方法: