2011-11-06 79 views
0

我有一個Web用戶控制,具有與在第二3列 1)的GridView在第一, 2)過濾器選項(文本框), 3)和另一個的GridView在第三表。從一個GridView的複製內容到另一個

當我在過濾器(文本框)中輸入文本時,某些行從數據庫中選擇並顯示在第三列的GridView中。這個GridView有一個選擇按鈕,當它被按下時,我希望該行(或只是它的一些列)被添加到第一列的GridView中。

從我聽說的,在這種情況下使用DataTable是很常見的。我有的代碼:

public partial class WebUserControl1 : System.Web.UI.UserControl 
{ 
    DataTable tempTable = new DataTable(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     tempTable.Columns.Add("ObjectID"); 
     tempTable.Columns.Add("Name"); 
     tempTable.Columns.Add("Price"); 
     currentDay.DataSource = tempTable; 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
    } 

    protected void objectChooser_SelectedIndexChanged(
     object sender, EventArgs e) 
    { 
     DataRow newRow = tempTable.NewRow(); 
     newRow["ObjectID"] = objectChooser.SelectedRow.Cells[0].Text; 
     newRow["Name"] = objectChooser.SelectedRow.Cells[1].Text; 
     newRow["Price"] = objectChooser.SelectedRow.Cells[2].Text;   

     tempTable.Rows.Add(newRow);       
     currentDay.DataBind(); 
    } 

    protected void Button2_Click(object sender, EventArgs e) 
    { 
     DataClasses1DataContext dc = new DataClasses1DataContext(); 

     var objects = from p in dc.VisitingObjects 
        where p.City == tbCityFilter.Text 
        select p; 

     objectChooser.DataSource = objects; 
     objectChooser.DataBind(); 
    } 
} 

但是,這是一個錯誤。當我第一次按下選擇按鈕(在GridView中)時,它可以工作(新的值被添加到第一個GridView,但在此之後,按下選擇按鈕只會改變第一個GridView中的值,但不會添加新行。請你告訴我什麼是錯我的代碼來,或許是使用複製值從GridView1到GridView2更好的辦法?

回答

1

I feel like I've said this before ...

您需要存儲的數據表的地方,不會消失在回發之間,會議可能是一個很好的開始。

objects = Session["data"]; //cast this 
if (objects == null) 
    //init objects to a new list of your obj type 
objects = objects.Union (
    //your code for filtering grid rows 
    ); 
Session["data"] = objects; 
相關問題