我在类中定义了一个dataTable:
def calculate_price(wood_type, drawers):
# wood_type = get_wood_type()
# drawers = get_drawers()
wood_price = 0
drawer_price = 0
# ... other calulations
return drawer_price + wood_price
def display_price(wood, drawer_count):
price = calculate_price(wood, drawer_count)
### Either do this here, or pass in via the main method below
# wood = get_wood_type()
# drawer_count = get_drawers()
print("The amount of drawers you requested were: ", drawer_count, ". Their total was ", drawer_count * 30, ".")
print("The type of would you requested was ", wood, ". The total for that was ", drawer_count * 30 - price, ".")
print("Your total is: ", price)
if __name__ == '__main__':
wood = get_wood_type()
drawer_count = get_drawers()
display_price(wood, drawer_count)
当我的主表单加载时,我创建了DataTable的结构,如下所示:
Public Shared Positions As DataTable
PositionX,PositionY和PositionZ的值随时间而变化,我想使用作为主键的hash值在DataTable中更新它们。
但是,我似乎无法正确理解语法。本文(https://msdn.microsoft.com/en-us/library/tat996zc.aspx)表明这种方法可行:
Function createTable()
' Function to create datatable structure
Dim table As New DataTable
' Define primary key
Dim keys(0) As DataColumn
Dim keyColumn As New DataColumn()
keyColumn.ColumnName = "hash"
keys(0) = keyColumn
' Define columns
table.Columns.Add(keyColumn)
table.Columns.Add("PositionX", GetType(String))
table.Columns.Add("PositionY", GetType(String))
table.Columns.Add("PositionZ", GetType(String))
return table
End Function
但将其应用于我的代码:
Dim customerRow() As Data.DataRow
customerRow = DataSet1.Tables("Customers").Select("CustomerID = 'ALFKI'")
customerRow(0)("CompanyName") = "Updated Company Name"
customerRow(0)("City") = "Seattle"
在前两行“Globals.Positions.DataRow not defined”和“Tables不是DataTable的成员”中给出错误。
我需要做什么才能获得包含我正在寻找的主键值的行,以便我可以更新一些列值?
答案 0 :(得分:1)
也许在DataTable的类中有一个属性,设置它,加载数据。有一种方法可以在一行中查找和设置值,例如
Public Class Demo1
Public Property DataTable As DataTable
''' <summary>
''' Fire off create and load tabe
''' </summary>
Public Sub New()
CreateTable()
LoadData()
End Sub
Public Sub CreateTable()
DataTable = New DataTable
DataTable.Columns.Add(New DataColumn With {.ColumnName = "hash", .DataType = GetType(String)})
DataTable.Columns.Add(New DataColumn With {.ColumnName = "PositionX", .DataType = GetType(String)})
DataTable.PrimaryKey = New DataColumn() {DataTable.Columns("hash")}
End Sub
Public Sub LoadData()
DataTable.Rows.Add(New Object() {"22DE43AS"})
DataTable.Rows.Add(New Object() {"44DE43AB"})
DataTable.Rows.Add(New Object() {"33DE43AW"})
End Sub
''' <summary>
''' Simple demo to find and update a row
''' </summary>
''' <param name="hash"></param>
''' <param name="PositionX"></param>
''' <returns></returns>
Public Function SetItem(ByVal hash As String, PositionX As String) As Boolean
Dim result As DataRow() = DataTable.Select($"hash = '{hash}'")
If result.Count > 0 Then
result.First.SetField(Of String)("PositionX", PositionX)
Return True
Else
Return False
End If
End Function
End Class
演示
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim demo As New Demo1
If demo.SetItem("44DE43AB", "new value") Then
MessageBox.Show("Success")
Else
MessageBox.Show("Failed")
End If
End Sub
答案 1 :(得分:1)
您可以改为使用Dictionary
:
Class Point3D
Public X, Y, Z As Integer
End Class
Sub test()
Dim Positions As New Dictionary(Of String, Point3D) ' create
Positions("44DE43AB") = New Point3D With {.X = 1, .Y = 2} ' add
Positions("44DE43AB").Z = 3 ' edit
Positions.Remove("44DE43AB") ' remove
End Sub