asp.net - 如何在 ASP.NET 中单击按钮时将文本框中的数据插入到 GridView 中

标签 asp.net gridview

我想从文本框中获取数据,然后单击按钮时我希望将该数据插入到 GridView 中。每次单击时都应该创建一个新行,并且不得删除旧行。每当我输入新数据并单击按钮时,我的旧行就会被删除,并存储新行来代替它。这是我的代码:

DataTable dt1 = new DataTable();
bool flag = false;  

private void gridVIEWData()
{
   dt1.Columns.Add("pName", typeof(string));
   dt1.Columns.Add("pCategory", typeof(string));
   dt1.Columns.Add("price", typeof(string));
   dt1.Columns.Add("pQuantity", typeof(string));
   dt1.Columns.Add("totalPrice", typeof(string));
}
protected void Button3_Click(object sender, EventArgs e)
{
    if (!flag)
    {
        gridVIEWData();
        flag = true;
        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
    else if (!IsPostBack)
    {
        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}

最佳答案

当您将旧数据与新数据绑定(bind)时,旧数据将被删除。您必须将旧数据保留在数据源中,即您的情况下的数据表中。通常我们有持久性介质来存储数据,例如文件数据库。

为了您的理解,我将在 session 中存储数据表,但通常不这样做,您应该存储在数据库或您喜欢的任何介质中。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridVIEWData();
        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}

private void gridVIEWData() {
   dt1.Columns.Add("pName", typeof(string));
   dt1.Columns.Add("pCategory", typeof(string));
   dt1.Columns.Add("price", typeof(string));
   dt1.Columns.Add("pQuantity", typeof(string));
   dt1.Columns.Add("totalPrice", typeof(string));
   Session["dtInSession"] = dt1;     //Saving Datatable To Session 
}


protected void Button3_Click(object sender, EventArgs e)
{
        if(Session["dtInSession"] != null)
             dt1 = (DataTable)Session["dtInSession"]; //Getting datatable from session 

        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        Session["dtInSession"] = dt1;     //Saving Datatable To Session 
        GridView1.DataSource = dt1;
        GridView1.DataBind();

    }
 }

关于asp.net - 如何在 ASP.NET 中单击按钮时将文本框中的数据插入到 GridView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12875643/

相关文章:

javascript - 将 jquery.slideToggle() 与 Knockout JS 结合使用

android - 如何在 XML 中填充 gridview

c# - 如何在gridview中动态添加html控件 "anchor tag"?

asp.net - 如何在asp.net中以编程方式编辑gridview?

asp.net - 显示 :block preventing font size change in ASP. 网络菜单

asp.net - 虚拟路径中带有点的 CSS 捆绑

c# - asp.net 文字不显示 <br><hr>

android - 我应该使用 GridView 还是 RecyclerView?

android - setOnItemClickListener() 无法在 Fragment 中的 gridView 上工作

c# - 如何计算 gridview asp.net c# 中的行数