2012-10-13 55 views
5

我想從文本框中獲取數據,並在按鈕單擊時我希望將該數據插入GridView。每次點擊都應該創建一個新行,並且舊行不能刪除。每當我輸入新的數據並點擊按鈕,我的舊行就會被刪除,新的行將被存儲在它的位置。這裏是我的代碼:如何將文本框中的數據插入到GridView上按鈕單擊ASP.NET

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

「Page_Load」中會發生什麼?考慮重新命名你的按鈕。 Button3的用途並不明顯。 –

回答

2

舊的數據被刪除,你有新的數據綁定。您必須將舊數據保留在數據源的數據表中。通常我們有用於存儲文件數據庫等數據的持久性介質。

爲了您的理解,我將在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(); 

    } 
} 
+0

thanx很多Adil現在它的工作。 。讚賞烏爾幫助.. :) –

+0

不客氣,但會議並不意味着這一點關於會議http://msdn.microsoft.com/en-us/library/ms178581(v=vs.100).aspx – Adil

+0

我認爲在Session中存儲數據表不是一個好主意。閱讀鏈接http://stackoverflow.com/questions/12811932/using-session-to-store-datatable –

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

可以ü請在我的代碼中更正它。或者我在哪裏打電話。 –

+0

現在即時獲取錯誤列'pName'不屬於表... :( –

+0

thanx很多@jams ...問題解決了,但我apreciate你的迴應和幫助... –

相關問題