windows Form  里面的DataGridView有的行进行了修改,还添加了两行,怎么用Update方法一次性保存到数据中?

解决方案 »

  1.   

    问题是你这个DataGridView里的数据是用怎么方式得来的?如果你是通过从[数据源]拖拽表到Form上生成的DataGridView及数据,那就用VS05自动生成的BindingNavigator进行增、删、改。通常你甚至连一行代码都不用写。另外,vs05开始强烈推荐使用BindingSource作为控件和数据之间的中间层。就是说控件绑定到BindingSource, BindingSource再绑定到数据对象(date item)或对象列表(data item list).
    这样做有许多好处。BindingSource.EndEdit();会把更新提交到内存中的对象或对象列表(如DataSet)。
    BindingSource.Update方法会把更新提交到数据库。总之,玩vs05的winform,一定关注BindingSource,没错的。BindingSource这东西绝对不是只提供一个中间层那么幼稚~ 一定多看些BindingSource, 多观察他为数据绑定,控件vs数据之间双向同步做出了多么‘伟大的’贡献:-)
      

  2.   

    我的数据是从Web Service里得来的, 怎么办呢?
      

  3.   

    datagridview的属性datasource不管是绑bindingsource还是直接datatable,当你编辑列表更新后,新行只要一通过datagridview的行校验(修改时通过单元格校验)之后该控件自动会把数据写到datatable里,当然包括了行状态,所以这是你在最后提交的时候只要关心datatable 里的行状态就OK 了,前台不用管!
      

  4.   

    看下Msdn,上面已讲得很清楚了...给你段上面的代码吧...
      

  5.   

    下面的完整代码示例提供的按钮用于从数据库重新加载数据和向数据库提交更改:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
    {
        private DataGridView dataGridView1 = new DataGridView();
        private BindingSource bindingSource1 = new BindingSource();
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        private Button reloadButton = new Button();
        private Button submitButton = new Button();    [STAThreadAttribute()]
        public static void Main()
        {
            Application.Run(new Form1());
        }    // Initialize the form.
        public Form1()
        {
            dataGridView1.Dock = DockStyle.Fill;        reloadButton.Text = "reload";
            submitButton.Text = "submit";
            reloadButton.Click += new System.EventHandler(reloadButton_Click);
            submitButton.Click += new System.EventHandler(submitButton_Click);        FlowLayoutPanel panel = new FlowLayoutPanel();
            panel.Dock = DockStyle.Top;
            panel.AutoSize = true;
            panel.Controls.AddRange(new Control[] { reloadButton, submitButton });        this.Controls.AddRange(new Control[] { dataGridView1, panel });
            this.Load += new System.EventHandler(Form1_Load);
            this.Text = "DataGridView databinding and updating demo";
        }    private void Form1_Load(object sender, System.EventArgs e)
        {
            // Bind the DataGridView to the BindingSource
            // and load the data from the database.
            dataGridView1.DataSource = bindingSource1;
            GetData("select * from Customers");
        }    private void reloadButton_Click(object sender, System.EventArgs e)
        {
            // Reload the data from the database.
            GetData(dataAdapter.SelectCommand.CommandText);
        }    private void submitButton_Click(object sender, System.EventArgs e)
        {
            // Update the database with the user's changes.
            dataAdapter.Update((DataTable)bindingSource1.DataSource);
        }    private void GetData(string selectCommand)
        {
            try
            {
                // Specify a connection string. Replace the given value with a 
                // valid connection string for a Northwind SQL Server sample
                // database accessible to your system.
                String connectionString =
                    "Integrated Security=SSPI;Persist Security Info=False;" +
                    "Initial Catalog=Northwind;Data Source=localhost";            // Create a new data adapter based on the specified query.
                dataAdapter = new SqlDataAdapter(selectCommand, connectionString);            // Create a command builder to generate SQL update, insert, and
                // delete commands based on selectCommand. These are used to
                // update the database.
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);            // Populate a new data table and bind it to the BindingSource.
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;            // Resize the DataGridView columns to fit the newly loaded content.
                dataGridView1.AutoResizeColumns( 
                    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            }
            catch (SqlException)
            {
                MessageBox.Show("To run this example, replace the value of the " +
                    "connectionString variable with a connection string that is " +
                    "valid for your system.");
            }
        }}
      

  6.   

    用access 举个例子//连接等
    OleDbDataAdapter oledbDa = new OleDbDataAdapter(selectCommand, oledbConnection);
    OleDbCommandBuilder cb = new OleDbCommandBuilder(oledbDa);
    oledbDa.Fill(datatable);
    //将数据放到datatable 中  datagridview 的数据源  是datatable
    //修改datagridview
    //更新
    oledbDa .Update(datatable);//datatable中要有数据库中的主键
    //在sql下就是把ole的东西改成sql