[英]Copy items from one GridView to another
我有一個Web用戶控件,該控件具有一個包含3列的表:1)首先是GridView,2)第二是過濾器選項(文本框),3)和第三是另一個GridView。
當我在filter(textbox)中輸入文本時,某些行將從數據庫中選擇出來,並顯示在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的復制值?
我覺得我之前已經說過 ...
您需要將DataTable存儲在回發之間不會消失的地方。 會議可能是一個很好的起點。
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.