简体   繁体   English

我无法在数据表中搜索一行并更新 vb.net 中的行单元格数据

[英]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.我正在编写 Inventory 程序并首次尝试使用 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.我有一个 function 可以找到当前行索引,但我不知道如何移动到该行并更新数据。 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.我想搜索作为自动增量记录 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前 1

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

ex 2前 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#. 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 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也就是说,我建议使用普通的 object,这将使此类操作变得非常简单

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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