DataGridView doesn't persist with new DataSource/DataBind assignment

Coreysan 1,631 Reputation points
2021-06-25T20:06:36.727+00:00

I hope I can learn about persistence in classic ASP.NET.

I have a project that doesn't seem to persist the data in my GridView (grid1). Basically I do this:

  1. My GridView is defined in aspx form.
  2. I included a button called "Update"
  3. Upon initialization, in the codebehind page load, I build a datatable (mydatatable) with 18 rows of data, and bind to grid1.
  4. When I use the form to indicate changes, I then click on the "Update" button.
  5. Inside the update_Click() method, I re-build mydatatable with 10 rows of data.
  6. grid1.DataSource = null;
  7. grid1.DataBind(); (Not sure if this is necessary)
  8. grid1.DataSource = mydatatable;
  9. grid1.DataBind();
  10. Focus returns to the same web form. (I don't close it, or redirect to some other page)

Here's the thing: if I click the "Update" button again, the grid1 has a count of 18 rows, not 10.

Why isn't grid1 persisting with 10 rows?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,249 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Albert Kallal 4,651 Reputation points
    2021-06-26T03:53:49.54+00:00

    Ok, I have a nice short example - and it lets you edit. One save for the whole grid.

    So, here is the simple markup.

    109480-image.png

    Ok, and here is our code to load up the grid (note how I persist the table by declaring a table at the form level (rstTable)

    ok, so the code to load up this grid is thus this:

        Dim rstTable As New DataTable  
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
      
            If IsPostBack = False Then  
                LoadGrid()  
            Else  
                rstTable = ViewState("rstTable")  
            End If  
      
        End Sub  
      
        Sub LoadGrid()  
      
            ' load up our grid  
            Using cmdSQL As New SqlCommand("SELECT * from tblHotels ORDER BY HotelName",  
                            New SqlConnection(My.Settings.TEST4))  
                cmdSQL.Connection.Open()  
                rstTable.Load(cmdSQL.ExecuteReader)  
                GridView1.DataSource = rstTable  
                GridView1.DataBind()  
      
            End Using  
            ViewState("rstTable") = rstTable  
    
        End Sub  
    

    And the result is this:

    109457-image.png

    So, now you can edit, tab around - editing anything - even check those boxes.

    Now, how to send/save the grid back to the database - the save button code for above?
    (saves the whole grid edit in one shot - this will include deletes, adds, and edits.

    Its is this:

       Protected Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click  
      
            ' pull repeater rows back to table.  
            For Each rRow As GridViewRow In GridView1.Rows  
      
                Dim OneDataRow As DataRow = rstTable.Rows(rRow.RowIndex)  
                OneDataRow.Item("HotelName") = CType(rRow.FindControl("HotelName"), TextBox).Text  
                OneDataRow.Item("FirstName") = CType(rRow.FindControl("FirstName"), TextBox).Text  
                OneDataRow.Item("LastName") = CType(rRow.FindControl("LastName"), TextBox).Text  
                OneDataRow.Item("City") = CType(rRow.FindControl("City"), TextBox).Text  
                OneDataRow.Item("Active") = CType(rRow.FindControl("Active"), CheckBox).Checked  
      
            Next  
            ' now send table back to database with updates, deletes and inserts  
            Using cmdSQL As New SqlCommand("SELECT * FROM tblHotels WHERE ID = 0",  
                                         New SqlConnection(My.Settings.TEST4))  
      
                cmdSQL.Connection.Open()  
                Dim daupdate As New SqlDataAdapter(cmdSQL)  
                Dim cmdBuild As New SqlCommandBuilder(daupdate)  
                daupdate.Update(rstTable)  
      
            End Using  
      
        End Sub  
    

    Again, not a lot of code. I think there are two nice tips here:
    note always - the on-load event. We only load the grid one time - (IsPostBack = False).

    Note how then then send grid back to table. And then note how we send the whole table back to the database in one shot.

    The other button? Well, that just adds a new row. But, since we persist the table? Then the code becomes very easy. The add row looks like this:

       Protected Sub cmdAdd_Click(sender As Object, e As EventArgs) Handles cmdAdd.Click  
      
            Dim MyOneRow = rstTable.NewRow  
            ' set defaults for this new row  
            MyOneRow("City") = "Edmonton"  
            MyOneRow("Active") = True  
      
            ' add htis new row to the table  
            rstTable.Rows.Add(MyOneRow)  
      
            ' now update the grid to show this new added row  
            GridView1.DataSource = rstTable  
            GridView1.DataBind()  
      
        End Sub  
    

    So note how we work this problem AGAINST the table. and then to update the grid - we do re-bind.
    And as noted, to save, we send grid to table, and then table to database.

    And the un-do button? Well, if add a few rows, edit other rows, if we don't hit save, and hit un-do, well, we just re-load the grid again. So that un-do button code is this:

        Protected Sub cmdUnDo_Click(sender As Object, e As EventArgs) Handles cmdUnDo.Click  
      
            LoadGrid()  
      
        End Sub  
      
    

    So it actually not a whole lot of code, and we have a great working grid, with edits, add row, and un-do.

    Now I suppose we should/could add some "x" button to the last column for a delete row. And again, that would be VERY easy!

    And, don't feel shy to ask for a c# version.

    So what really makes all this work was persiting that table.

    And I think perhaps we could/should add a delete buttion.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments