Reputation: 3224
I have a web user control, that has a table with 3 columns 1) GridView in first, 2) filter option (textbox) in second, 3) and another GridView in third.
When I enter text in filter(textbox) some rows gets selected from database and are shown in GridView in third column. This GridView has select button, when it gets pressed, I want that row (or just some columns from it) to be added to GridView in first column.
From what I've heard, it is common to use DataTable in theese situations. The code that I have:
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();
}
}
But something is wrong with this. When I press select button (in GridView) first time, it works (new value gets added to first GridView, but after that, pressing select button just changes the value in first GridView, but no new rows are added.
Could you please tell me what is wrong with my code, or maybe there is better way to use copy values from GridView1 to GridView2?
Upvotes: 0
Views: 3260
Reputation: 3360
I feel like I've said this before...
You need to store your DataTable somewhere that doesn't disappear between postbacks. The Session might be a good place to start.
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;
Upvotes: 1