views:

7265

answers:

2

I get a DataTable from a DataSet and then bind that DataTable to a DataGridView. Once the user edits the information on the DataGridView how do I take those changes and put them back into a DataTable that was used that I can then put back into my DataSet?

I want to make a Save Button on my DataGrid that when pressed actually saves the changes.

I don't if I can get anymore specific than that, because it is a fairly simple question.

Thanks in advance!

Let me know if you need me to elaborate more.

+2  A: 

If you are using data-binding to a DataGridView, then you are already updating the DataTable / DataSet. If you mean changes down to the database, then that is where adapters come into play.

Here's an example:

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();

        DataSet set = new DataSet();
        DataTable table = set.Tables.Add("MyTable");
        table.Columns.Add("Foo", typeof(int));
        table.Columns.Add("Bar", typeof(string));

        Button btn;
        using (Form form = new Form
        {
            Text = "DataGridView binding sample",
            Controls =
            {
                new DataGridView {
                    Dock = DockStyle.Fill,
                    DataMember = "MyTable",
                    DataSource = set
                },
                (btn = new Button {
                    Dock = DockStyle.Bottom,
                    Text = "Total"
                })
            }
        })
        {
            btn.Click += delegate
            {
                form.Text = table.AsEnumerable().Sum(
                    row => row.Field<int>("Foo")).ToString();
            };
            Application.Run(form);
        }

    }
}
Marc Gravell
My god you know what you are talking about! Will you please look at my other question and help me out it seems you may be my only hope! http://stackoverflow.com/questions/518239/c-issue-what-is-the-simplest-way-for-me-to-load-a-mdb-file-make-changes-to-it. Thanks! Ill see if this works for me.
OneShot
That answered my question right there though. Whatever DataTable i've bound to the DataGrid is already being changed. Now if I could only figure out how to put that DataTable back into the Access .MDB file it came from. :/ Thanks!
OneShot
Well, I'll take a look, but caveat: I know "not a lot" about mdb or using DataSet in anger.
Marc Gravell
A: 

as mentioned DataAdapters are one of the easy ways.

See: http://www.codeproject.com/KB/database/relationaladonet.aspx

for a pretty simple example that I think covers what youu need.

kpollock