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

时间:2012-10-13 18:17:05

标签: 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();
    }
}

2 个答案:

答案 0 :(得分:2)

将旧数据与新数据绑定时,旧数据将被删除。您必须将旧数据保留在数据源中的数据表中。通常我们有持久的媒介来存储数据,如文件数据库。

为了您的理解,我会在会话中存储数据表,但通常不会实践,您应该存储在数据库中或者您喜欢的媒体中。

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();

    }
 }

答案 1 :(得分:1)

这是因为您每次在回发时都会调用gridVIEWData();再次创建表列。

试试这个

DataTable dt1 = new DataTable();

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 Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridVIEWData();
        GridView1.DataSource = dt1;
        GridView1.DataBind();
        Session["dt1"]=dt1;
    }
}

protected void Button3_Click(object sender, EventArgs e)
{
    if(Session["dt1"]!=null) dt1 = (DataTable) Session["dt1"];
    Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
    DataRow dr = dt1.NewRow();
    dr["pName"] = DropDownList2.SelectedItem.Text;
    dr["pCategory"] = DropDownList1.SelectedItem.Text;
    dr["price"] = txt_price.Text;
    dr["pQuantity"] = txt_quantity.Text;
    dr["totalPrice"] = total.ToString();
    dt1.Rows.Add(dr);

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