Datagrid Update - Rows Lost

Discussion in 'ASP .Net Web Controls' started by Demetri, Nov 15, 2003.

  1. Demetri

    Demetri Guest

    I have a datagrid on an aspx page with one column added via property builder - the edit/update/cancel. In addition there is a dropdownlist which has a list of table names.

    Upon selecting one of the table names a post back is fired. During the post back the datagrid has columns added to it dynamically to represent each of the columns in the selected table

    When the user clicks the edit button on a given row the cells then have textboxes added for user input

    When the user clicks update after having made their changes a postback is fired and I attempt reference the textboxes only to find that there are NO cells any longer much less textboxes to reference in an effort to retrieve values to populate parameters for an update command object

    If I rebind the datagrid before making the references to the textboxes it then contains the data BEFORE the user edited it therefore I lose their changes

    Any ideas how I can keep from losing the textboxes to reference

    And yes, I have enableviewstate set to true for BOTH the page and the datagrid specifically.
     
    Demetri, Nov 15, 2003
    #1
    1. Advertising

  2. Demetri

    Aaron Weiker Guest

    On Sat, 15 Nov 2003 15:56:05 -0800, Demetri wrote:
    > If I rebind the datagrid before making the references to the textboxes
    > it then contains the data BEFORE the user edited it therefore I lose
    > their changes.


    Demetri,
    The order in which things are bound will make a difference in this case.
    First of all you will want to make sure that you only use DataBind() after
    you have done everything with the data that was there previously (posted).
    This means that you will want to handle the update then do the databind. In
    order to provide more specifics it would be beneficial if you could post
    any relevant code.

    --
    Aaron Weiker
    http://aaronweiker.com
     
    Aaron Weiker, Nov 16, 2003
    #2
    1. Advertising

  3. Demetri

    Demetri Guest

    order to provide more specifics it would be beneficial if you could post
    any relevant code.

    -- --------------------------------------------------------

    Private Sub dgTableData_UpdateCommand(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs) Handles dgTableData.UpdateCommand
    Update(ddlTableNames.SelectedItem.Value, e.Item.ItemIndex)
    dgTableData.EditItemIndex = -1
    BindGrid(ddlTableNames.SelectedItem.Value)
    End Sub

    Private Sub Update(ByVal table As String, ByVal index As Integer)
    Dim sql As String = "select * from " & table
    Dim da As New SqlDataAdapter(sql, myConnection)
    da.SelectCommand.CommandText = sql
    Dim cb As New SqlCommandBuilder(da)
    Dim cmdUpdate As SqlCommand
    Dim i As Integer
    Dim txtBox As TextBox
    Dim p As SqlParameter

    cmdUpdate = cb.GetUpdateCommand

    For i = 1 To dgTableData.Items(index).Cells.Count - 1 '--skip the first cell (0 = link buttons)
    txtBox = CType(dgTableData.Items(index).Cells(i).Controls(0), TextBox)
    For Each p In cmdUpdate.Parameters
    If p.SourceColumn = dgTableData.Columns(i).HeaderText Then
    p.Value = txtBox.Text
    End If
    Next
    Next

    myConnection.Open()
    cmdUpdate.ExecuteNonQuery()
    myConnection.Close()
    End Sub

    Private Sub BindGrid(ByVal table As String)
    If table <> "" Then
    Dim sql As String = "select * from " & table '--Build select based on passed in table name.
    Dim da As New SqlDataAdapter(sql, myConnection)
    Dim ds As New DataSet
    Dim c As DataColumn

    da.Fill(ds)

    Dim si As StateItem
    For Each si In viewstate.Keys
    Trace.Warn(si.Value)
    Next
    With dgTableData
    .EnableViewState = True
    '--Create and add columns for each column in the table.
    For Each c In ds.Tables(0).Columns
    Dim bc As New BoundColumn
    With bc
    .DataField = c.ColumnName
    .HeaderText = c.ColumnName
    End With
    '.Columns.AddAt(c.Ordinal, bc)
    .Columns.Add(bc)
    Next
    '--Bind Data.
    .DataSource = ds
    .DataBind()
    End With

    End If
    End Sub
     
    Demetri, Nov 16, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGVtZXRyaQ==?=

    Datagrid Update - Rows Lost

    =?Utf-8?B?RGVtZXRyaQ==?=, Nov 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    399
    Aaron Weiker
    Nov 16, 2003
  2. mesut
    Replies:
    0
    Views:
    693
    mesut
    Feb 19, 2007
  3. Erik Lautier

    Update Multiple Datagrid Rows

    Erik Lautier, Apr 8, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    532
    Erik Lautier
    Apr 8, 2008
  4. Andreas Klemt
    Replies:
    0
    Views:
    333
    Andreas Klemt
    Oct 8, 2003
  5. Demetri

    Datagrid Update - Rows Lost

    Demetri, Nov 15, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    134
    Aaron Weiker
    Nov 16, 2003
Loading...

Share This Page