• Viernes 19 de Abril de 2024, 01:09

Autor Tema:  Coger valores actualizados de un Cell  (Leído 1384 veces)

kaidok

  • Miembro activo
  • **
  • Mensajes: 33
    • Ver Perfil
Coger valores actualizados de un Cell
« en: Martes 9 de Diciembre de 2008, 19:39 »
0
Partiendo del siguiente código:

Código: Text
  1.  
  2.  
  3.    Sub ItemsGrid_Cancel(sender As Object, e As DataGridCommandEventArgs)
  4.  
  5.         ' Set the EditItemIndex property to -1 to exit editing mode.
  6.         ' Be sure to rebind the DateGrid to the data source to refresh
  7.         ' the control.
  8.         DataGrid1.EditItemIndex = -1
  9.         DataGrid1.DataBind()
  10.  
  11.      End Sub
  12.  
  13.    Sub ItemsGrid_Update(sender As Object, e As DataGridCommandEventArgs)
  14.  
  15.     ' For bound columns, the edited value is stored in a TextBox.
  16.        ' The TextBox is the 0th control in a cell's Controls collection.
  17.        ' Each cell in the Cells collection of a DataGrid item represents
  18.        ' a column in the DataGrid control.
  19.  
  20.  
  21.        Dim qtyText As TextBox = CType(e.Item.Cells(3).Controls(0), TextBox)
  22.        Dim priceText As TextBox = CType(e.Item.Cells(4).Controls(0), TextBox)
  23.  
  24.        ' Retrieve the updated values.
  25.        Dim item As String = e.Item.Cells(2).Text
  26.        Dim qty As String = qtyText.Text
  27.        Dim price As String = priceText.Text
  28.  
  29.        Dim dr As DataRow
  30.  
  31.        Dim Cart As DataTable=Ds.Tables(0)
  32.        Dim CartView As New DataView(Cart)
  33.  
  34.        ' With a database, use an update command to update the data.
  35.        ' Because the data source in this example is an in-memory
  36.        ' DataTable, delete the old row and replace it with a new one.
  37.  
  38.        ' Remove the old entry and clear the row filter.
  39.        CartView.RowFilter = "Id='" & item & "'"
  40.        If CartView.Count > 0 Then
  41.  
  42.           Cart.Rows(0).Delete()
  43.  
  44.        End If
  45.        CartView.RowFilter = ""
  46.  
  47.        ' ***************************************************************
  48.        ' Insert data validation code here. Be sure to validate the
  49.        ' values entered by the user before converting to the appropriate
  50.        ' data types and updating the data source.
  51.        ' ***************************************************************
  52.  
  53.        ' Add the new entry.
  54.        dr = Cart.NewRow()
  55.        dr(0) = item
  56.        dr(1) = qty
  57.  
  58.        ' If necessary, remove the '$' character from the price before
  59.        ' converting it to a Double.
  60.  
  61.  
  62.        dr(2) = price
  63.  
  64.  
  65.  
  66.        Cart.Rows.Add(dr)
  67.  
  68.        ' Set the EditItemIndex property to -1 to exit editing mode.
  69.        ' Be sure to rebind the DateGrid to the data source to refresh
  70.        ' the control.
  71.        DataGrid1.EditItemIndex = -1
  72.  
  73.        Adap.Update(Ds,"Tabla1")
  74.        DataGrid1.DataBind()
  75.        Ds.AcceptChanges()
  76.  
  77.  
  78.       End Sub
  79.  
  80.  
  81.  

Donde Adap es un DataAdapter rellenado, Ds un DataSet y DataGrid1 el DataGrid.

Me gustaría saber por que al tomar los datos mediante el uso e.Items.Cells no cojo como se encuentra el TextBox en ese mismo instante sino el valor inicial que tenía, haciendo imposible por tanto cualquier actualización. Está comprobado además que el error ha de estar relacionado con ese punto, ya que por ejemplo si asigno un valor directamente, (por ejemplo dr(2)="prueba", la actualización sí que se realiza correctamente.

¿Hay alguna operación extra que deba hacer para que con e.Items.Cells coja el valor actual y no el inicial?

Un saludo.

sergiotarrillo

  • Moderador
  • ******
  • Mensajes: 1059
    • Ver Perfil
    • http://sergiot2.com/blog
Re: Coger valores actualizados de un Cell
« Respuesta #1 en: Domingo 11 de Enero de 2009, 07:53 »
0
Haz probado usando FindControl?

Revisa este artículo: Adding a New Record to the DataGrid.

Saludos,


Sergio Tarrillo
Blog]miBlog[/url]

No me visiten!