【问题标题】:My dataset doesn't work properly我的数据集无法正常工作
【发布时间】:2015-12-28 17:28:38
【问题描述】:

我的问题是,当它更新时,它会一次又一次地添加以前的数据

我使用 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 = 我的连接字符串

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();
    }
}

}

【问题讨论】:

    标签: c# winforms telerik dataset


    【解决方案1】:

    您似乎正在使用全局变量来保留从网格中读取的值。如果在第一次插入后不清除它们,则全局列表中的值仍然存在,然后将它们再次添加到数据表中

    当然,您可以只使用一个循环来重新加载具有网格中实际值的全局变量

    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);
        }
        ....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多