小编Ume*_*zad的帖子

如何在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) …
Run Code Online (Sandbox Code Playgroud)

asp.net gridview

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net ×1

gridview ×1