如何在Windows应用程序中更新gridview值? [英] how to Update gridview value in windows application?

查看:66
本文介绍了如何在Windows应用程序中更新gridview值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void Update_Click(object sender, EventArgs e)
      {

          for (int i = 0; i < dataGridView2.RowCount - 1; i++)
          {
              string sql = "UPDATE [SAPProduction].[ProductionData] SET " + " Jobcard= '" + dataGridView2.Rows[i].Cells["Jobcard"].Value.ToString() + "', " +
                                 " Operator = '" + dataGridView2.Rows[i].Cells["Operator"].Value.ToString() + "', " +
                                 " Supervisor = '" + dataGridView2.Rows[i].Cells["Supervisor"].Value.ToString() + "' , " +
                                 " Profilename ='" + dataGridView2.Rows[i].Cells["Profilename"].Value.ToString() + "' , " +
                                 " Process='" + dataGridView2.Rows[i].Cells["Process"].Value.ToString() + "' , " +
                                 " Date='" + dataGridView2.Rows[i].Cells["Date"].Value.ToString() + "' , " +
                                 " Remark='" + dataGridView2.Rows[i].Cells["Remark"].Value.ToString() + "' , " +
                                 " Approvedby='" + dataGridView2.Rows[i].Cells["Approvedby"].Value.ToString() + "' , " +
                                 " Division ='" + dataGridView2.Rows[i].Cells["Division"].Value.ToString() + "'  " +
                                 " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  "; // Jobcard = your desire Rows Jobcard Id

              SqlCommand cmd = new SqlCommand(sql, objConn1);
              objConn1.Open();
              cmd.ExecuteNonQuery();
              objConn1.Close();
              //MessageBox.Show("Record Update successfully...");
          }
      }







i使用了以上查询但没有工作




i have used above query but not working

推荐答案

Vinayak,



代码的执行是一个方向,即从屏幕到数据库。如果你想要一个双向行为,你必须将数据源附加到datagridview并使用该数据源(数据源可以是DataTable,DataSet或DataAdaptor)来执行后端操作,这很容易与网格本身混乱。 />


检查以下链接并尝试

http://forums.devshed.com/net-development-87/update-table-in-database-through-datagridview-in -winform-350445.html [ ^ ]





SqlDataAdapter da = new SqlDataAdapter( select * from product,strcon);

SqlCommandBuilder cb = new SqlCommandBuilder(da);

DataSet ds = new DataSet();

da.Fill(ds);

dataGridView1.DataSource = ds.Ta BLES [0];



//现在你可以保存对后端的更改

da.Update(ds);
Vinayak,

the execution of your code is one direction i.e from Screen to database. If you want a two way behavior you must attach a data source to your datagridview and use that data source ( data source can be a DataTable,DataSet or DataAdaptor) to execute the backend operation thats pretty easy rather messing with the grid itself.

check the following link and try that
http://forums.devshed.com/net-development-87/update-table-in-database-through-datagridview-in-winform-350445.html[^]


SqlDataAdapter da=new SqlDataAdapter("select * from product",strcon);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource=ds.Tables[0];

//now u can save changes to back end with
da.Update(ds);


这篇关于如何在Windows应用程序中更新gridview值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆