win form程序,以下代码将执行存储过程的结果绑定到datagridview,
datagridview可编辑,编辑后点击保存按钮,请问怎样更新回数据库?
谢谢!BindingSource bs = new BindingSource();
SqlConnection cn = new SqlConnection("[连接串]");
            SqlCommand cm = new SqlCommand("pro_loaddata", cn);
            cm.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt;
                dt = ds.Tables[0];
                bs.DataSource = dt;
                dg.DataSource = bs;
            }

解决方案 »

  1.   

    private void datagridview1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                int i,j,k;            if (e.RowIndex != -1)
                {
                    i = (int)datagridview1.Rows[e.RowIndex].Cells[0].Value;
                    j = (int)datagirdview1.Rows[e.RowIndex].Cells["1"].Value;
                    k = (int)datagridview1.Rows[e.RowIndex].Cells["2"].Value;
                    tbTableAdapter.Update(i,si,j,k,sj);
                }
            }这个方法很笨但是能解决。我不知道有什么好方法
      

  2.   

    http://zhidao.baidu.com/question/30621321.html
      

  3.   

    this.DataGridView.SelecedItem as T直接更新数据库就可以了。
      

  4.   

    我用的最笨的方法,用SQL 的Update 语句来更新的
      

  5.   

     SqlConnection cn = new SqlConnection("[连接串]");
                SqlCommand cm = new SqlCommand("pro_loaddata", cn);             dg.EndEdit();
                 bs.EndEdit();             Adapter.SelectCommand = new SqlCommand(sql, cn);
                 SqlCommandBuilder builder  = new SqlCommandBuilder(Adapter);
                Adapter.UpdateCommand = builder.GetUpdateCommand();
                DataView tab = bs.List as DataView;
                DataTable table = tab.Table;
                Adapter.Update(table);