源码
//保存 private void update_Click(object sender, EventArgs e) { gridView1.CloseEditor(); gridView1.UpdateCurrentRow(); DataTable dt = (DataTable)gridControl1.DataSource; //选中行行号 int a = gridView1.FocusedRowHandle; if ( GSR_Fid() != "") { if (MessageBox.Show("确实要保存该行吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes) { String connsql = "server=localhost.;database=M10;integrated security=SSPI"; SqlConnection MyConnection = new SqlConnection(connsql); string sql_update = "Update m_task_001 set FItemID='" + dt.Rows[a]["FItemID"] + "',FBillNo='" + dt.Rows[a]["FBillNo"] + "',FStatus='" + dt.Rows[a]["FStatus"] + "',FPQty='" + dt.Rows[a]["FPQty"] + "'" + " where FID=" + "'" + GSR_Fid() + "'"; SqlCommand MyCommand = new SqlCommand(sql_update, MyConnection); try { MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close(); MessageBox.Show("成功修改信息!"); } catch (Exception ex) { //写入TXT文本 string Route = @"D:\1.txt"; StreamWriter sw = File.AppendText(Route); sw.Write(ex.Message + " " + DateTime.Now.ToString() + "\r\n"); sw.Close(); MessageBox.Show("错误信息:" + ex.Message, "出现错误"); return; } } } else { if (MessageBox.Show("确实要保存该行吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes) { String MyConn = "server=localhost.;database=M10;integrated security=SSPI"; SqlConnection MyConnection = new SqlConnection(MyConn); string sql_insert = "insert into m_task_001 (FItemID,FBillNo,FStatus,FPQty) values ('" + dt.Rows[a]["FItemID"] + "','" + dt.Rows[a]["FBillNo"] + "','" + dt.Rows[a]["FStatus"] + "','" + dt.Rows[a]["FPQty"] + "')"; SqlCommand MyCommand = new SqlCommand(sql_insert, MyConnection); try { MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close(); MessageBox.Show("成功添加信息!"); } catch (Exception ex) { MessageBox.Show("错误信息:" + ex.Message, "出现错误"); } } } CNT(); }