我现在先从数据库里取出标准表到datagridview里,
修改完后我想将该表存到数据库里,但不覆盖原来的标准表,应该怎么做?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace NAVECOrepair
{
public partial class 结算单 : Form
{
SqlConnection conn;
DataSet deptDataSet;
DataSet deptDataSet1;
SqlDataAdapter adapter;
SqlDataAdapter adapter1;
string connectString = DateTime.Now.ToString();//用于获取当前时间作为3个表的连接,精确到秒
private bool lcaBool = false;
public 结算单()
{
InitializeComponent();
}
private void 结算单_Load(object sender, EventArgs e)
{
LCALoad();//加载人工成本分析
MCALoad();//加载材料成本分析
}
//加载人工成本分析
private void LCALoad()
{
conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["partialConnectString"].ToString();
SqlCommand cmd = new SqlCommand("select l_number,l_type,workHours,exaWorkHours,l_unitPrice,unitPriceTerms,exaUnitPriceTerms,connectString from LCA where connectString='0'", conn);
adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
deptDataSet = new DataSet();
adapter.Fill(deptDataSet, "lca");
DataView dv = new DataView(deptDataSet.Tables["lca"]);
this.dgvLCA.DataSource = dv;
this.dgvLCA.Columns[0].HeaderText = "序号";
this.dgvLCA.Columns[1].HeaderText = "加工类型";
this.dgvLCA.Columns[2].HeaderText = "工时";
this.dgvLCA.Columns[3].HeaderText = "审核工时";
this.dgvLCA.Columns[4].HeaderText = "单价(元)";
this.dgvLCA.Columns[5].HeaderText = "单价项";
this.dgvLCA.Columns[6].HeaderText = "审核单价项";
this.dgvLCA.Columns[7].HeaderText = "连接串";
this.dgvLCA.Columns[0].ReadOnly = true;//设置只允许修改第3,4列的值
this.dgvLCA.Columns[1].ReadOnly = true;
this.dgvLCA.Columns[2].ReadOnly = false;
this.dgvLCA.Columns[3].ReadOnly = false;
this.dgvLCA.Columns[4].ReadOnly = true;
this.dgvLCA.Columns[5].ReadOnly = true;
this.dgvLCA.Columns[6].ReadOnly = true;
//this.dgvLCA.Columns[7].Visible=false;//设置连接串不显示
this.dgvLCA.Columns[0].Width = 60;
for (int i = 0; i < dgvLCA.Rows.Count; i++)
{
dgvLCA.Rows[i].Cells[7].Value = connectString;
}
}
//加载材料成本分析
private void MCALoad()
{
conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["partialConnectString"].ToString();
SqlCommand cmd1 = new SqlCommand("select m_number,m_name,m_type,unit,quantity,m_unitPrice,m_totalPrice,remark,connectString from mca", conn);
adapter1 = new SqlDataAdapter();
adapter1.SelectCommand = cmd1;
deptDataSet1 = new DataSet();
adapter1.Fill(deptDataSet1, "mca");
DataView dv1 = new DataView(deptDataSet1.Tables["mca"]);
this.dgvMCA.DataSource = dv1;
this.dgvMCA.Columns[0].HeaderText = "序号";
this.dgvMCA.Columns[1].HeaderText = "材料名称";
this.dgvMCA.Columns[2].HeaderText = "型号规格";
this.dgvMCA.Columns[3].HeaderText = "单位";
this.dgvMCA.Columns[4].HeaderText = "数量";
this.dgvMCA.Columns[5].HeaderText = "单价(元)";
this.dgvMCA.Columns[6].HeaderText = "总价(元)";
this.dgvMCA.Columns[7].HeaderText = "备注";
this.dgvMCA.Columns[8].HeaderText = "连接串";
//this.dgvMCA.Columns[8].Visible = false;//设置连接串不显示
this.dgvMCA.Columns[0].Width = 60;
for (int i = 0; i < dgvMCA.Rows.Count; i++)
{
dgvMCA.Rows[i].Cells[8].Value=connectString;
}
}
//当单元格内容改变时,修改单价项或者审核单价项
private void dgvLCA_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1 && !lcaBool)
{
lcaBool = true;
if (this.dgvLCA.Rows[e.RowIndex].Cells[2].Value.ToString() != "")
{
this.dgvLCA.Rows[e.RowIndex].Cells[5].Value = Convert.ToDecimal(this.dgvLCA.Rows[e.RowIndex].Cells[2].Value) * Convert.ToDecimal(this.dgvLCA.Rows[e.RowIndex].Cells[4].Value);
}
if (this.dgvLCA.Rows[e.RowIndex].Cells[3].Value.ToString() != "")
{
this.dgvLCA.Rows[e.RowIndex].Cells[6].Value = Convert.ToDecimal(this.dgvLCA.Rows[e.RowIndex].Cells[3].Value) * Convert.ToDecimal(this.dgvLCA.Rows[e.RowIndex].Cells[4].Value);
}
lcaBool = false;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["partialConnectString"].ToString();
}
}
}