C# 如何在DataTable中现有行的下方添加新行

C# 如何在DataTable中现有行的下方添加新行,c#,asp.net,C#,Asp.net,每次单击按钮时,我都试图将一个新行添加到我的数据表,然后将其绑定到网格视图(查询)。当我第一次单击按钮时,将添加该行。但是,当我试图通过再次单击来添加另一行时,它将替换现有行 protected void addInquiry_Click(object sender, EventArgs e) { try { Quantity = QuantityTxt.Text; string Details = Request.Fo

每次单击按钮时,我都试图将一个新行添加到我的
数据表
,然后将其绑定到
网格视图
查询
)。当我第一次单击按钮时,将添加该行。但是,当我试图通过再次单击来添加另一行时,它将替换现有行

protected void addInquiry_Click(object sender, EventArgs e)
{
      try
      {
             Quantity = QuantityTxt.Text;
             string Details = Request.Form[Products.UniqueID];
             SelectedProduct = Details.Split('!');
             ProductNo = SelectedProduct[0];
             ProductDescription = SelectedProduct[1];
             ProductSapPack = SelectedProduct[2];
             ProductID = SelectedProduct[3];
             DataTable dt = new DataTable();                       
             dt.Columns.AddRange(new DataColumn[5] { 
                                 new DataColumn("ProductID",typeof(int)),
                                 new DataColumn("ProductNo", typeof(string)),
                                 new DataColumn("Description", typeof(string)),
                                 new DataColumn("SapPack",typeof(string)),
                                 new DataColumn("Quantity",typeof(string)),
                                 });

            DataRow dr = dt.NewRow();

            dr[0] = ProductID;
            dr[1] = ProductNo;
            dr[2] = ProductDescription;
            dr[3] = ProductSapPack;
            dr[4] = Quantity;

            dt.Rows.Add(dr);
            Inquiries.DataSource = dt;
            Inquiries.DataBind();
      }

      finally
      {
            QuantityTxt.Text = String.Empty;
            Description.Text = String.Empty;
            SapPack.Text = String.Empty;
            Products.Text = String.Empty;
      }
}

修改代码如下:将创建的数据表添加到会话状态,然后在用户单击按钮时尝试检索它

protected void addInquiry_Click(object sender, EventArgs e)
{
     DataTable dt=(DataTable)Session["Inquiry"];
  try
  {
         Quantity = QuantityTxt.Text;
         string Details = Request.Form[Products.UniqueID];
         SelectedProduct = Details.Split('!');
         ProductNo = SelectedProduct[0];
         ProductDescription = SelectedProduct[1];
         ProductSapPack = SelectedProduct[2];
         ProductID = SelectedProduct[3];
         if(dt==null)
         {
             dt= new DataTable();                       
         }             
         dt.Columns.AddRange(new DataColumn[5] { 
                             new DataColumn("ProductID",typeof(int)),
                             new DataColumn("ProductNo", typeof(string)),
                             new DataColumn("Description", typeof(string)),
                             new DataColumn("SapPack",typeof(string)),
                             new DataColumn("Quantity",typeof(string)),
                             });

        DataRow dr = dt.NewRow();

        dr[0] = ProductID;
        dr[1] = ProductNo;
        dr[2] = ProductDescription;
        dr[3] = ProductSapPack;
        dr[4] = Quantity;

        dt.Rows.Add(dr);
        Inquiries.DataSource = dt;
        Inquiries.DataBind();
        Session.Add("Inquiry",dt);
  }
  finally
  {
        QuantityTxt.Text = String.Empty;
        Description.Text = String.Empty;
        SapPack.Text = String.Empty;
        Products.Text = String.Empty;
  }
}

由于您没有在数据库中保存数据,因此确实需要创建会话/视图状态来保存数据表

if(Session["Products"] != null)
 dt = (DataTable) Session["Products"]; 
else
 dt = new DataTable(); // For the first time creates a new DataTable otherwise reads from Session variable

----
At the end, save the data to the Session variable:
Session["Products"] = dt;

对我有效的答案是

protected void addInquiry_Click(object sender, EventArgs e)
{
     DataTable dt=(DataTable)Session["Inquiry"];
  try
  {
         Quantity = QuantityTxt.Text;
         string Details = Request.Form[Products.UniqueID];
         SelectedProduct = Details.Split('!');
         ProductNo = SelectedProduct[0];
         ProductDescription = SelectedProduct[1];
         ProductSapPack = SelectedProduct[2];
         ProductID = SelectedProduct[3];
         if(dt==null)
         {
             dt= new DataTable(); 
             dt.Columns.AddRange(new DataColumn[5] { 
                             new DataColumn("ProductID",typeof(int)),
                             new DataColumn("ProductNo", typeof(string)),
                             new DataColumn("Description", typeof(string)),
                             new DataColumn("SapPack",typeof(string)),
                             new DataColumn("Quantity",typeof(string)),
                             });

         }             


        DataRow dr = dt.NewRow();

        dr[0] = ProductID;
        dr[1] = ProductNo;
        dr[2] = ProductDescription;
        dr[3] = ProductSapPack;
        dr[4] = Quantity;

        dt.Rows.Add(dr);
        Inquiries.DataSource = dt;
        Inquiries.DataBind();
        Session.Add("Inquiry",dt);
  }
  finally
  {
        QuantityTxt.Text = String.Empty;
        Description.Text = String.Empty;
        SapPack.Text = String.Empty;
        Products.Text = String.Empty;
  }
}

每次单击该按钮时,您都在创建一个新的
数据表
。因此,实际上它不是在更新旧的行,而是每次都在创建新的行。因此,我应该在click事件之外创建DataTable实例?@RahulSingh我在click事件之外创建了DataTable实例,但它仍然替换了我的原始行!理想情况下,您应该这样做,但这不起作用,因为页面在每次回发后都会实例化,这意味着您的数据表将再次创建。如果您真的想这样做,那么您可能可以将DataTable存储在ViewState变量中,但这不是最好的方法。我正在使用Ajax更新我的DataTable和Gridview!当我这样做时,如果(dt==null){dt=new DataTable();dt.Columns.AddRange(new DataColumn[5]{new DataColumn(“ProductID”,typeof(int)),new DataColumn(“ProductNo”,typeof(string)),新数据列(“说明”,类型(字符串)),新数据列(“SAPACK”,类型(字符串)),新数据列(“数量”,类型(字符串)),})