I have a page with two datagrids. One reads the value from a database and allows selection. When the user selects a grid it adds the data to the second datagrid. This is the code I am using for this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dc1 As New DataColumn("NAME")
Dim dc2 As New DataColumn("PRICE")
dt.Columns.Add(dc1)
dt.Columns.Add(dc2)
Dim dr1 As DataRow = dt.NewRow()
GridView2.DataSource = dt
GridView2.DataBind()
End Sub
and
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
Dim dr1 As DataRow = dt.NewRow()
dr1(0) = Name.ToString
dr1(1) = Price.ToString
dt.Rows.Add(dr1)
GridView2.DataSource = dt
GridView2.DataBind()
Now, this works perfectly.... but this obviously would work under a forms service. In a web based enviroment whenever a user selects a value in Grid 1 it resets the values in Grid 2 and I need that multiple rows are added to the 2nd Datagrid every time the user selects a value. The "data adding" is fired when the selectedindexchanged event takes place in GridView1. I think I need to save the data to a session/cookie but I have no idea how would I do this in the case of a datagrid. Like, what should I save and how I'd read it. Both Gridviews are under an Update Panel control so Postback should be instantaneous (if needed).
Sightly updated code...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
dt = DirectCast(Session("Purchase"), DataTable)
Else
Dim dc1 As New DataColumn("NAME")
Dim dc2 As New DataColumn("PRICE")
dt.Columns.Add(dc1)
dt.Columns.Add(dc2)
Dim dr1 As DataRow = dt.NewRow()
GridView2.DataSource = dt
GridView2.DataBind()
End If
End Sub
and
Dim dr1 As DataRow = dt.NewRow()
Session.Add("Purchase", dt)
dr1(0) = Name.ToString
dr1(1) = Price.ToString
dt.Rows.Add(dr1)
dt = DirectCast(Session("Purchase"), DataTable)
GridView2.DataSource = dt
GridView2.DataBind()
datagridin the session you would save thedatatableand set thedatasourceof thedatagridto thedatatableeach time a record is added.Session.Add("Key", dt)to save it to the session anddirectcast(Session("Key"), datatable)to read it back.Session.Add("Purchase", dt)after the dr1 is declared. Then in pageload I added thisIf Not IsPostBack Then dt = DirectCast(Session("Purchase"), DataTable) Else... code. Still the same result. Am I doing it wrong?