正在替换而不是更新DataGridView1

本文关键字:更新 DataGridView1 替换 | 更新日期: 2023-09-27 18:00:55

我写了一个函数来更新数据网格视图,但它被替换而不是附加。

我先前声明如下:;

List<LineItem> Lines = new List<LineItem>(); // Creates the list of LINES
 public class LineItem{
       private string _itemNumber;
       private string _description;
       private string _unitPrice;
       private string _quantity;
       private string _netWeight;
       private string _netPrice;

       public string itemNumber
        {
            get
            {
                return _itemNumber;
            }
            set
            {
                _itemNumber = value;
            }
        }
       public string description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }
       public string unitPrice
        {
            get
            {
                return _unitPrice;
            }
            set
            {
                _unitPrice = value;
            }
        }
       public string quantity
        {
            get
            {
                return _quantity;
            }
            set
            {
                _quantity = value;
            }
        }
       public string netWeight
        {
            get
            {
                return _netWeight;
            }
            set
            {
                _netWeight = value;
            }
        }
       public string netPrice
        {
            get
            {
                return _netPrice;
            }
            set
            {
                _netPrice = value;
            }
        }

以下是不起作用的功能;

protected void Button10_Click(object sender, EventArgs e)
    {
        if (TextBox16.Text == "")
        {
            Label20.Visible = true;
            Label20.Text = "Must enter a quantity!";
        }
        else
        {
            if (TextBox15.Text == "")
            {
            }
            else
            {
                int tempInt01 = Int32.Parse(TextBox16.Text);
                GlobalVariables.qlQuantity = tempInt01;
                GlobalVariables.qlItemNumber = TextBox15.Text;
                Label20.Visible = false;
                TextBox15.Text = "";
                TextBox16.Text = "";
                try
                {
                    string connectionString = "Data Source=DBSERVER01;Initial Catalog=TCM95;Integrated Security=False;User ID=sa;Password=foobar";
                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        connection.Open();
                        SqlCommand command = new SqlCommand("SELECT omi.ITMMAS_BASE.ID_ITEM, omi.ITMMAS_BASE.DESCR_1, omi.ITMMAS_LOC.PRICE_SELL_1, omi.ITMMAS_BASE.WGT_ITEM FROM omi.ITMMAS_BASE INNER JOIN omi.ITMMAS_COST ON omi.ITMMAS_BASE.ID_ITEM = omi.ITMMAS_COST.ID_ITEM INNER JOIN omi.ITMMAS_LOC ON omi.ITMMAS_BASE.ID_ITEM = omi.ITMMAS_LOC.ID_ITEM WHERE (omi.ITMMAS_BASE.ID_ITEM = '" + GlobalVariables.qlItemNumber + "') AND (omi.ITMMAS_LOC.ID_LOC = '11')", connection);
                        SqlDataReader myDataReader = command.ExecuteReader();
                        while (myDataReader.Read())
                        {
                            LineItem a1 = new LineItem();  // CREATES NEW Line Item
                            int tempInt0 = myDataReader.GetOrdinal("ID_ITEM");
                            GlobalVariables.qlItemNumber = myDataReader.GetString(tempInt0);
                            a1.itemNumber = GlobalVariables.qlItemNumber;
                            int tempInt1 = myDataReader.GetOrdinal("DESCR_1");
                            GlobalVariables.qlDescription = myDataReader.GetString(tempInt1);
                            a1.description = GlobalVariables.qlDescription;
                            int tempInt2 = myDataReader.GetOrdinal("PRICE_SELL_1");
                            GlobalVariables.qlPrice = myDataReader.GetDecimal(tempInt2);
                            a1.unitPrice = GlobalVariables.qlPrice.ToString();
                            int tempInt3 = myDataReader.GetOrdinal("WGT_ITEM");
                            GlobalVariables.qlWeight = myDataReader.GetDecimal(tempInt3);
                            a1.netWeight = GlobalVariables.qlWeight.ToString();
                            GlobalVariables.netWeight = GlobalVariables.netWeight + (GlobalVariables.qlWeight * GlobalVariables.qlQuantity);
                            a1.quantity = GlobalVariables.qlQuantity.ToString();
                            GlobalVariables.totalQuantity = GlobalVariables.totalQuantity + GlobalVariables.qlQuantity;
                            a1.netPrice = a1.unitPrice;
                            Lines.Add(a1);

                        }
                        connection.Close();
                    }
                }
                catch (SqlException ex)
                {
                    Label20.Visible = true;
                    Label20.Text = ex.ToString();

                }
                TextBox6.Text = GlobalVariables.netWeight.ToString();
                TextBox14.Text = GlobalVariables.totalQuantity.ToString();
                GridView1.Visible = true;// Sets visibility on Datalist1.
                GridView1.DataSource = Lines;
                GridView1.DataBind();
            }
        } // end of else for IF TEXTBOX16..
    }

基本上,我只希望Button10函数将LineItem添加到List并附加到DataGridView1,而不是完全覆盖它(就像它现在所做的那样!(。

正在替换而不是更新DataGridView1

要了解您的问题,请阅读.Net页面生命周期。。。它会教你在每次页面加载、回发等时代码会发生什么…

每次加载页面时,您声明列表的地方基本上都会被清除(例如,当您单击浏览器上的按钮时,它会启动回发,并且您的整个类都是从头开始"重新创建"的(。ASP.Net.中没有真正的"页面特定全局变量">

因此,对于您来说,您将希望使用Session对象或ViewState对象以全局方式存储该用户、该页面的列表。

希望这能有所帮助。