我的问题是,当它被更新时,它会一次又一次地添加之前的数据
我使用 telerik网格视图
这里是我的3层代码
第一个
private void btnSbmt_Click(object sender, EventArgs e)
{
foreach (var row in radGridView1.Rows)
{
_MyName.Add((string)row.Cells[1].Value);
}
foreach (var row in radGridView1.Rows)
{ // 0 - first column
_MyAmount.Add((int)row.Cells[2].Value);
}
foreach (var row in radGridView1.Rows)
{
_MyPrice.Add((decimal)row.Cells[3].Value);
}
Ref_View_Model = new View_model._View_Model();
Ref_View_Model.GetInsertProduct(_myName, _myAmount, _myPrice, txtDt.Text);
radGridView1.CurrentRow.Delete();
productTableAdapter.Update(sales_and_Inventory_SystemDataSet);
productTableAdapter.Fill(sales_and_Inventory_SystemDataSet.Product);
MessageBox.Show("Product(s) were added", "Done", MessageBoxButtons.OK);}
第二个
public void GetInsertProduct( List<string> _name, List<int> _amount, List<decimal> _price, string _date)
{
Ref_Model = new Model._Model();
Ref_Model.InsertProduct( _name, _amount, _price, _date);
}
和第三个
public void InsertProduct(List<string> _myName,
List<int> _myAmount,
List<decimal> _myPrice, string _date)
{ Connection_String = myconnection string
Query = @"INSERT INTO dbo.product(Name, Amount, Price, [date])
VALUES(@Name, @Amount, @Price, @Date);";
using ( Con = new SqlConnection(Connection_String))
using ( Cmd = new SqlCommand(Query, Con))
{
Cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
Cmd.Parameters.Add("@Amount", SqlDbType.Int);
Cmd.Parameters.Add("@Price", SqlDbType.Decimal);
// Cmd.Parameters.Add("@Date", SqlDbType.NVarChar);
Cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = Convert.ToDateTime(_date);
Cmd.Connection = Con;
Con.Open();
int recordsToAdd = _myName.Count();
for(int x = 0; x < recordsToAdd; x++)
{
Cmd.Parameters["@Name"].Value = _myName[x];
Cmd.Parameters["@Amount"].Value = _myAmount[x];
Cmd.Parameters["@Price"].Value = _myPrice[x];
Cmd.Parameters["@Date"].Value = _date;
Cmd.ExecuteNonQuery();
}
}
}
答案 0 :(得分:0)
您似乎正在使用全局变量来保留从网格中读取的值。如果您在第一次插入后没有清除它们,那么您仍然拥有全局列表中的值,并将它们再次添加到数据表中
当然,您只需使用一个循环就可以使用网格中的实际值重新加载全局变量
private void btnSbmt_Click(object sender, EventArgs e)
{
// This removes whatever is in the lists
_MyName.Clear();
_MyAmount.Clear();
_MyPrice.Clear();
// and now start adding items from scratch
foreach (var row in radGridView1.Rows)
{
_MyName.Add((string)row.Cells[1].Value);
_MyAmount.Add((int)row.Cells[2].Value);
_MyPrice.Add((decimal)row.Cells[3].Value);
}
....