DataGridView1正在被替换而不是更新

时间:2014-04-30 19:08:31

标签: c# datagridview

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

我之前声明了以下内容;

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,而不是完全覆盖它(就像现在这样做!)。

1 个答案:

答案 0 :(得分:1)

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

您声明列表的位置,每次页面加载时基本上都会被清除干净(例如,当您单击浏览器上的按钮时,它会启动回发,并且您的整个类被“重新创建”刮)。 ASP.Net中没有真正的“特定于页面的全局变量”。

因此,对于您而言,您将希望以全局方式使用Session对象或ViewState对象为该用户存储列表。

希望这会有所帮助。