简体   繁体   中英

I can't search a Datatable for one row and update row cell data in vb.net

I'm writing in Inventory program and making my first attempt in utilizing a DataTable. All I wanted to do is find the right row and be able to update columns like costs, sale price, current inventory. I've tried so many examples that I've found but I just can't seem to make it work. I have a function that can find current row index but I don't know how to move to that row and update the data. I've tried selects statements several different ways and haven't been successful. I wanna search the Item which is the auto increment record ID. This can't be that difficult but I've spent three days circling. If I can get a little advice I would appreciate it. Thank you

    InvtTable.Columns.Add("Item", Type.GetType("System.Int32"))      ' 1  12
    InvtTable.Columns.Add("ModelStyle", Type.GetType("System.String")) ' 3  2
    InvtTable.Columns.Add("Description", Type.GetType("System.String")) ' 4  3
    InvtTable.Columns.Add("CoverFinsh", Type.GetType("System.String")) ' 5  4
    InvtTable.Columns.Add("Invt.Type", Type.GetType("System.String"))   ' 6  5
    InvtTable.Columns.Add("Tag No.", Type.GetType("System.String"))     ' 7  6
    InvtTable.Columns.Add("Loc", Type.GetType("System.String"))         ' 8  7
    InvtTable.Columns.Add("Curr.Invt.", Type.GetType("System.Int32"))   ' 9  8
    InvtTable.Columns.Add("PO", Type.GetType("System.String"))          ' 10 9
    InvtTable.Columns.Add("PO Date", Type.GetType("System.String"))     ' 11 10
    InvtTable.Columns.Add("CompID", Type.GetType("System.Int32"))     ' 11 10
    InvtTable.Columns.Add("Vendor", Type.GetType("System.String"))     ' 11 10
    InvtTable.Columns.Add("Cost", Type.GetType("System.Int32"))     ' 11 10
    InvtTable.Columns.Add("Retail", Type.GetType("System.Int32"))     ' 11 10
    InvtTable.Columns.Add("Sale", Type.GetType("System.Int32"))     ' 11 10
    InvtTable.Columns.Add("Note", Type.GetType("System.String"))     ' 11 10
    InvtTable.Columns.Add("Width", Type.GetType("System.Int32"))     ' 11 10
    InvtTable.Columns.Add("Length", Type.GetType("System.Int32"))     ' 11 10

Here are some of the things I've tried. EX 1

Dim rows As DataRow() = InvtTable.[Select]("Item = '%0%' ", tbxItemID.Text)
For Each row As DataRow In rows
    MsgBox("here")
Next

ex 2

Dim matches = From row In InvtTable
                      Let Item = row.Field(Of Int32)("Item")
                      Where Item = Decimal.Parse(tbxItemID.Text)

There's so many more examples for c#. I wish I would have went that direction but I really don't wanna start again. I hope this makes enough sense to help me. Thank you again

Alternative way with Linq

Dim id As Integer = Integer.Parse(tbxItemID.Text)
Dim row As DataRow = 
    table.AsEnumerable().First(Function(row) row.Field(Of Integer)("Item") = id

' Update row
row.SetField("Cost", 99)
row.SetField("Sale", 199)

That said, I would suggest to use plain object, which will make such operations effectively simple

Public Class InventoryItem
    Public Property Id As Integer
    Public Property ModelStyle As String
    Public Property CurrentSaldo As Integer
    Public Property CostPrice As Integer
    Public Property SalePrice As Integer
    ' and so on
End Class

Than you can populate collection of items from database or file or anywhere else

Dim inventory As List(Of Inventory) = LoadFromDatabase()

Dim id As Integer = Integer.Parse(tbxItemID.Text)
Dim item = inventory.First(Function(item) item.Id = id)

With item
    .CostPrice = 99
    .SalePrice = 199
    .CurrentSaldo -= 1
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM