Lord Relix
Lord Relix

Reputation: 942

Dynamically adding multiple rows to a ASP Datagrid

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()

Upvotes: 0

Views: 884

Answers (2)

Mike_OBrien
Mike_OBrien

Reputation: 1423

Try this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'This will load the DT into the session when the page initially loads and then skip it on subsequent page loads.
    If Not IsPostBack Then
        Dim dc1 As New DataColumn("NAME")
        Dim dc2 As New DataColumn("PRICE")
        dt.Columns.Add(dc1)
        dt.Columns.Add(dc2)
        Session.Add("Purchase", dt)
        GridView2.DataSource = dt
        GridView2.DataBind()
    End If
End Sub

GridView1_SelectedIndexChanged:

'Clear out the old datasource.
GridView2.Datasource = Nothing
GridView2.DataBind()
'Pull the datatable from the session variable
dt = DirectCast(Session("Purchase"), DataTable)
'Create the new row and set the values
Dim dr1 As DataRow = dt.NewRow()
dr1(0) = Name.ToString
dr1(1) = Price.ToString
'Add the row to the table
dt.Rows.Add(dr1)
'If the session variable exists(which is should) then we overwrite it, if not we create it.
If Session("Purchase") Is Nothing Then Session.Add("Purchase", dt) Else Session("Purchase") = dt
'Reset the datasource of the datagrid
GridView2.DataSource = dt
GridView2.DataBind()

One thing I've run into doing this before is that in order for the datasource to actually update as expected I needed to do GridView2.Datasource = Nothing and GridView2.DataBind() as the very first thing in the event handler, I'm not sure why but it works when I do that and it doesn't when I don't so I didn't spend the time looking into it.

Upvotes: 1

Marcianin
Marcianin

Reputation: 461

Seems to me like the problem is that when you select the first value, it creates a postback and that resets the other grid, you could disable EnableAutoPostback on your original element but i would like to know how are you manipulating the selected data.

I think you can easily accomplish this if instead of using 2 grids use one, add a column with a checkbox to grid named 'selected' and then save the selected row ids. Are you saving the selected rows to a database or how are you going to manipulate the selected rows? Another possibility is to use JQuery Drag and Drop (which in my opinion would be the most elegant solution but would require more time to code) with that, you can drag rows from one grid to another and then save the selected rows.

Upvotes: 0

Related Questions