none
Updating DataTable fields RRS feed

  • Question

  •  I have the following code in a GridView "RowUpdating" sub:

    --------------------------------------------------------------------------------------------
    Dim NewValue As Decimal = CType(CType(Me.grdData.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text, Decimal)

    MetDs = Session(
    "MetDset")
    Dim Query = _
        From r In MetDs.Tables(0) _
          Where r.Field(Of Int64)(0) = CType(Me.grdData.Rows(e.RowIndex).Cells(1).Text, Int64) _
            And r.Field(Of Int64)(1) = CType(Me.grdData.Rows(e.RowIndex).Cells(2).Text, Int64) _
        Select r

    For Each row In Query
      '  MsgBox(row.Field(
    Of Decimal)(2).ToString)
      '  I want to set the value of "row.Field(Of Decimal)(2)" to be equal to "NewValue"
    Next
    --------------------------------------------------------------------------------------------

    I would like to use the "For Each Row in Query" to update the value of a field in the DataTable.

    What is the best way to do this? I'm very new to ASP.Net VB2008 and LINQ.

    Wednesday, January 21, 2009 5:27 PM

Answers

  • My misunderstanding.

    ' .Single() get the first row (and make sure there is only 1 row)
    ' .SetField to change the value on the row
    Query.Single().SetField(of Decimal)(2, NewValue)

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Wednesday, January 21, 2009 7:33 PM
    Moderator

All replies

  • To clarify, you have a DataColumn with .DataType = GetType(System.Decimal)
                    and you want it to store the string "NewValue".

    a) You could change your column's .DataType = GetType(System.Object)
         and store both Decimal and String values, but then row.Field(Of Decimal)(#) will throw when you encounter a string

    b) You could change your column's .DataType = GetType(System.String)
         and store everything as string, accessing values as row.Field(of String)(...)
         then you could change the value via row.SetField(of String)(#, "NewValue")


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Wednesday, January 21, 2009 6:59 PM
    Moderator
  • No, I don't want to store a string, I want to store the contents of the NewValue variable (i.e., Dim NewValue As Decimal = CType(CType(Me.grdData.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text, Decimal)).
     
    So, ultimately, what needs to be stored in the DataColumn is CType(CType(Me.grdData.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text, Decimal)
    Wednesday, January 21, 2009 7:23 PM
  • My misunderstanding.

    ' .Single() get the first row (and make sure there is only 1 row)
    ' .SetField to change the value on the row
    Query.Single().SetField(of Decimal)(2, NewValue)

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Wednesday, January 21, 2009 7:33 PM
    Moderator
  • Nice--thank you!
    Wednesday, January 21, 2009 9:25 PM