我一直在同一行插入数据

本文关键字:一行 插入 数据 我一直在 | 更新日期: 2023-09-27 18:18:13

请帮助找出我做错了什么,我的datagridview是绑定的,所以我不能添加一个新的行,所以我已经创建了一个数据表能够这样做,有一个问题,我无法摆脱,因为我试图添加数据我添加到同一行。请帮助了解为什么会发生这种情况,而不是新的行,或者一个更好的方法来绕过它。

public void getAllData(string stockCode, string description, string unit, string quantity, string costPrice,
          string saleprice, string taxcode)
{
    DataTable table = new DataTable();
    DataRow dr = table.NewRow();
    table.Columns.Add(new DataColumn("stk_code"));
    table.Columns.Add(new DataColumn("stk_description"));
    table.Columns.Add(new DataColumn("quantity"));
    table.Columns.Add(new DataColumn("uom"));
    table.Columns.Add(new DataColumn("discount"));
    table.Columns.Add(new DataColumn("tax_code"));
    table.Columns.Add(new DataColumn("stk_sale_price_one"));
    dr[0] = stockCode;
    dr[1] = description;
    dr[2] = quantity;
    dr[3] = 0;
    dr[4] = unit;
    dr[5] = taxcode;
    dr[6] = saleprice;
    invoiceItemDataView.DataSource = table;
    table.Rows.Add(dr);
    table.AcceptChanges();
}

我一直在同一行插入数据

为什么每次设置行时都要创建表列?你不需要那样做。在程序开始时做一次。就像这样。

创建表:

  private DataTable CreateDataTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add(new DataColumn("stk_code"));
        table.Columns.Add(new DataColumn("stk_description"));
        table.Columns.Add(new DataColumn("quantity"));
        table.Columns.Add(new DataColumn("uom"));
        table.Columns.Add(new DataColumn("discount"));
        table.Columns.Add(new DataColumn("tax_code"));
        table.Columns.Add(new DataColumn("stk_sale_price_one"));
        return table;
    }

将其初始化为一个DataTable,并将其设置为DataGridView:

   private DataTable dt;
    public MyClass()
    {
        InitializeComponent();
        dt = CreateDataTable();
    }

现在,把你的数据表发送给你的方法。

getAllData(dt, "1", "2", "2", "2",
                  "2", "s", "s");
            dataGridView1.DataSource = dt;

最后,向Datatable中插入新行:

public void getAllData(DataTable table, string stockCode, string description, string unit, string quantity, string costPrice,
      string saleprice, string taxcode)
    {
        int index = table.Rows.Count > 0 ? table.Rows.Count : 0;
        DataRow dr = table.NewRow();
        dr[0] = stockCode;
        dr[1] = description;
        dr[2] = quantity;
        dr[3] = 0;
        dr[4] = unit;
        dr[5] = taxcode;
        dr[6] = saleprice;
        table.Rows.InsertAt(dr, index);
        table.AcceptChanges();
    }

        public void getData(string stockCode, string description, string unit, string quantity, string costPrice,
          string saleprice, string taxcode)
{
    DataTable table = new DataTable();
    DataRow dr = table.NewRow();
    table.Columns.Add(new DataColumn("stk_code"));
    table.Columns.Add(new DataColumn("stk_description"));
    table.Columns.Add(new DataColumn("quantity"));
    table.Columns.Add(new DataColumn("uom"));
    table.Columns.Add(new DataColumn("discount"));
    table.Columns.Add(new DataColumn("tax_code"));
    table.Columns.Add(new DataColumn("stk_sale_price_one"));
    dr[0] = stockCode;
    dr[1] = description;
    dr[2] = quantity;
    dr[3] = 0;
    dr[4] = unit;
    dr[5] = taxcode;
    dr[6] = saleprice;

    table.Rows.Add(dr);
    invoiceItemDataView.DataSource = table;
}
static DataTable MyTable = null; //store the table data;
//may be you means "AddData"?
public void AddData(string stockCode, string description, string unit, string quantity, string costPrice,
          string saleprice, string taxcode)
{
    DataTable table = MyTable
    if(table == null)
    {
        DataTable table = new DataTable();    
        table.Columns.Add(new DataColumn("stk_code"));
        table.Columns.Add(new DataColumn("stk_description"));
        table.Columns.Add(new DataColumn("quantity"));
        table.Columns.Add(new DataColumn("uom"));
        table.Columns.Add(new DataColumn("discount"));
        table.Columns.Add(new DataColumn("tax_code"));
        table.Columns.Add(new DataColumn("stk_sale_price_one"));
        MyTable = table;
    }
    DataRow dr = table.NewRow();
    dr[0] = stockCode;
    dr[1] = description;
    dr[2] = quantity;
    dr[3] = 0;
    dr[4] = unit;
    dr[5] = taxcode;
    dr[6] = saleprice;
    table.Rows.Add(dr);
    table.AcceptChanges();
    invoiceItemDataView.DataSource = table;
}