动态创建可自动清除的数据行。 [英] Dynamically create datable row going to clear automatically.

查看:66
本文介绍了动态创建可自动清除的数据行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

对不可理解的标题抱歉。

我在asp.net网站上工作,我需要创建动态数据表。在这里有一个按钮和按钮点击事件我必须在datatabel.at中添加一行每次按钮点击事件触发我必须在数据表中追加一行。

为此我做这个代码。



Hello
sorry for ununderstandable title.
I work on asp.net website in this i need to create dynamic datatable. in this there one button and on button click event i have to add one row in datatabel.at every time button click event fired i have to append one more row in datatable.
For that i do this code.

//Form load event datatable initialize.

    DataTable dt = new DataTable();
    DataRow dtrow;
    protected void Page_Load(object sender, EventArgs e)
    {
        
            dt.Columns.Add("Productid", typeof(string));
            dt.Columns.Add("ProductName", typeof(string));
            dt.Columns.Add("ProductsubName", typeof(string));
            dt.Columns.Add("UnitPrice", typeof(string));
            dt.Columns.Add("Qyt", typeof(string));
            dt.Columns.Add("TotalAmount", typeof(string));
             
        
    }
//now on button click event

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string id = ComboBox2.SelectedItem.Value.ToString();
        dataBind objdata = new dataBind();
        DataTable dtprod= objdata.viewdata("Select * from product_master where sno='"+id+"'");

        dtrow = dt.NewRow();
           // Create New Row
        dtrow["Productid"] = id;            //Bind Data to Columns
        dtrow["ProductName"] = dtprod.Rows[0]["prod_type"];
        dtrow["ProductsubName"] = dtprod.Rows[0]["prod_sub_type"];
        dtrow["UnitPrice"] = dtprod.Rows[0]["rate"];
        dtrow["Qyt"] = txtqty.Text.ToString();
        dtrow["TotalAmount"] = dtprod.Rows[0]["rate"];
        dt.Rows.Add(dtrow);

        GridView1.DataSource = dt; //Bind the gridview with datatable.
        GridView1.DataBind();

}





现在问题是第一次按钮点击它工作正常但是当我点击第二次数据表gonig上的按钮清除,只插入新数据行。但实际上我想在数据表中添加行



请帮我解决这个问题。



Now problem is for 1st time button click it's work fine but when i click button on second time datatable gonig to clear and only new data row is inserted. but actually i want to append row in datatable

please help me to solve out this.

推荐答案

对DataTable和DataRow使用静态关键字

Use Static Keyword for DataTable and DataRow
static DataTable dt = new DataTable();
static DataRow dtrow;





还可以使用......



And also use...

protected void Page_Load(object sender, EventArgs e)
{

     if(!page.IsPostBack)
     {
        dt.Columns.Add("Productid", typeof(string));
        dt.Columns.Add("ProductName", typeof(string));
        dt.Columns.Add("ProductsubName", typeof(string));
        dt.Columns.Add("UnitPrice", typeof(string));
        dt.Columns.Add("Qyt", typeof(string));
        dt.Columns.Add("TotalAmount", typeof(string));
     }

}





希望此帮助



Hope This Help


您好,



全局声明数据表并将数据表保持在viewstate状态。



每次添加行时,将行添加到viewstate的数据表而不是本地数据表。在回发之间你会丢失数据。



Ex:



DataTable dt = new DataTable()< br $>


ViewStatt [OriginalData] = dt



你有没有?



问候,

Kiran。
Hi,

Declare the datatable globally and keep the datatable in viewstate.

while every time adding row,add row to datatable of viewstate not to local datatable. between postbacks you will loss the data.

Ex:

DataTable dt=new DataTable()

ViewStatt["OriginalData"]= dt

Did you got it?

Regards,
Kiran.


你好,



首先:



将您的Page_Load代码包含在
Hello there,

First:

Wrap your Page_Load code in
if(!page.IsPostBack)
         {}



其次,您需要将DataTable保存在Session或ViewState对象中,就像在当前场景中一样,每次按钮单击都会创建一个新的 DataTable dt 对象。

所以在此之后这样:


Second, you need to keep your DataTable in a Session or ViewState object as in the current scenario every button click will create a new DataTable dt object.
So go like this after this:

DataTable dtprod= objdata.viewdata("Select * from product_master where sno='"+id+"'");

line

DataTable dtObj = null;
            if (Session["YourSessionKey"] != null)
            {
                dtObj= (DataTable) Session["YourSessionKey"];
            }
            else
            {
                dtObj = new DataTable();
            }

并在点击事件中使用 dtObj 而不是 dt 并在此行之后:

and use dtObj in the click event instead of dt and after this line :

dt.Rows.Add(dtrow);

添加这些语句

dtObj.AcceptChanges();
            Session["YourSessionKey"] = dtObj;





最后,删除此行DataTable dt = new DataTable();并将自己的名字命名为Session key YourSessionKey



希望它有所帮助。

祝你好运


Azee ......



Finally, remove this line DataTable dt = new DataTable(); and give your own name to Session key YourSessionKey.

Hope it helps.
Good luck

Azee...


这篇关于动态创建可自动清除的数据行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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