单击按钮时如何从“文本框"将数据插入GridView中. [英] how to insert the data in GridView from Textboxes on button click.

查看:79
本文介绍了单击按钮时如何从“文本框"将数据插入GridView中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在过去2天一直在解决该问题.我想从文本框中获取数据,然后单击按钮,我希望将该数据插入到gridview中.每次单击都会创建一个新行,并且绝不能删除旧行.这是我的代码.每当我输入新数据并单击按钮时,我的旧行就会被删除,而新行将代替它存储.请帮助我解决这个问题.


hi i have been stuck on that problem for last 2 days. i want to get the data from textboxes and on button click i want that data to be inserted in gridview. on every click there shud be a new row created and old row must not be removed. here is my code. whenever i enter the new data and click on button my old row is deleted and new row is stored in place of it. Kindly help me with my that problem thanx.


        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 GridView1_SelectedIndexChanged(object sender, EventArgs e)
       {

       }

       protected void TextBox4_TextChanged(object sender, EventArgs e)
       {

       }

       protected void txt_productCode_TextChanged(object sender, EventArgs e)
       {

       }

       protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
       {

       }

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

推荐答案

为什么会发生此问题,因为Asp.Net无法在不同请求之间持久化您在DataTable中创建的更改.您需要编写代码以保留这些更改.一种解决方案是在添加新行之后将数据表存储在Session变量中.在添加下一行之前,还请确保从此会话中检索相同的数据表.

该代码将如下所示;
Why this problem happens is Asp.Net cannot persist the changes in DataTable you created between different requested. You need to write code to persist these changes. One solution is to store the data table in a Session variable after adding the new row. Also make sure to retrieve the same data table from this session before adding the next row.

The code will look like this;
DataTable dt1=new DataTable();
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               Session["a"] = null;
           }
       }



protected void Button3_Click(object sender, EventArgs e)
{
if  Session["a"]==null)
            {
                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);
 Session["datas"]=dt1;
                GridView1.DataSource = dt1;
                GridView1.DataBind();
            }
            else             {
                Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
DataTable dtcached = (DataTable)Session["a"];
                DataRow dr = dtcached.NewRow();
                dr["pName"] = DropDownList2.SelectedItem;
                dr["pCategory"] = DropDownList1.SelectedItem;
                dr["price"] = txt_price.Text;
                dr["pQuantity"] = txt_quantity.Text;
                dr["totalPrice"] = total;
DataTable dtCached=(DataTable)Session["datas"];
                dtCached.Rows.Add(dr);
 
                GridView1.DataSource = dtCached;                GridView1.DataBind();
            }
}


更改了代码,我已经用粗体显示了它,希望对您有所帮助.使用大量会话变量并不是很好,因为它会给服务器增加更多的负载,但是会给您一个提示


Changed code I have shown it in BOLD, Hope this helps.. It is not good to use lot of session variable as it adds more loads to server, but it gives you a hint


谢谢很多朋友.我自己解决了.现在我在删除行中遇到了问题.任何人都可以帮忙.
thanks alot buddy. i have solved it myself. now i m getting problem in deleting a row from it.can anyone help.


这篇关于单击按钮时如何从“文本框"将数据插入GridView中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆