我是VB.net中数据表的新手。我想创建一个具有预设行数和列数的数据表。我已经把那部分放下了(我想?)。现在我想填充每个"细胞"在数据表中包含我拥有的值。我已经看到了示例代码,它们在每行中填充数据,因为它们添加了新行。这不是我想要的方式,我也不确定我是否可以这样做,考虑我的应用程序。填充数据表后,我想将其原样写入CSV文件。但是,在我获得数据表部分工作之后,那就是。
感谢您的帮助
Dim bools() As Boolean = New Boolean(10) {testTypeNS, testTypeOR, torqueTypeBreak, torqueTypeFix, sheaveHigh, sheaveLow, _
directionCW, directionCCW, pneuActuateAuto, elecActuateAuto, hydrActuateAuto}
Dim Presets() As Integer = New Integer(16) {cyclesSP, oilFlowSP, oilTempSP, spindleSpeedSP, accelRateSP, decelRateSP, flowDevAlrmSP, _
oilTempAlrmSP, partTempAlrmSP, exitOilTempAlrmSP, spindleOverrunSP, OverrunHoldTimeSP, _
actuatorOnRpmSp, actuatorOffRpmSP, cycleIncrRpmSP, recordOnRpmSP, recordOffRpmSP}
Dim i As Integer = 0
Dim csv As String = "myfile.csv"
Dim sourceTable As DataTable = New DataTable()
sourceTable.Columns.AddRange(New DataColumn() {New DataColumn("BooleanValues", GetType(Boolean)), _
New DataColumn("IntValues", GetType(Integer)), _
New DataColumn("singValues", GetType(Single)), _
New DataColumn("accelVals", GetType(Integer)), _
New DataColumn("decelVals", GetType(Integer)), _
New DataColumn("speedVals", GetType(Integer)), _
New DataColumn("timeVals", GetType(Integer)), _
New DataColumn("flowVals", GetType(Integer)), _
New DataColumn("tempVals", GetType(Integer))})
For i = 0 To 49
'Add 50 rows to the Data Table
sourceTable.Rows.Add()
Next
For Each [bool] In bools
'Populate first column of cells in 'sourceTable' with boolean values that are in bools(10) array
Next
For Each [integer] In Presets
'Populate second column of cells in 'sourceTable' with integers that are in Presets(16) array.
Next
'etc.. Continue populating the other columns of the DataTable with corresponding array of values
答案 0 :(得分:0)
这实际上非常简单。将数据表转换为CSV的代码不是我的。你可以找到here。
Private Sub SaveRecipeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveRecipeToolStripMenuItem.Click
Dim testTypeNS As Boolean, testTypeOR As Boolean, torqueTypeBreak As Boolean, torqueTypeFix As Boolean, sheaveHigh As Boolean, _
sheaveLow As Boolean, directionCW As Boolean, directionCCW As Boolean, pneuActuateAuto As Boolean, elecActuateAuto As Boolean, _
hydrActuateAuto As Boolean
Dim cyclesSP As Integer, oilFlowSP As Integer, oilTempSP As Integer, spindleSpeedSP As Integer, accelRateSP As Integer, decelRateSP As Integer, _
flowDevAlrmSP As Integer, oilTempAlrmSP As Integer, partTempAlrmSP As Integer, exitOilTempAlrmSP As Integer, spindleOverrunSP As Integer, _
OverrunHoldTimeSP As Integer, actuatorOnRpmSp As Integer, actuatorOffRpmSP As Integer, cycleIncrRpmSP As Integer, recordOnRpmSP As Integer, _
recordOffRpmSP As Integer
Dim torqueAlrmSP As Single
cyclesSP = CInt(dgvPresets(1, 0).Value)
oilFlowSP = CInt(dgvPresets(1, 1).Value)
oilTempSP = CInt(dgvPresets(1, 2).Value)
spindleSpeedSP = CInt(dgvPresets(1, 3).Value)
accelRateSP = CInt(dgvPresets(1, 4).Value)
decelRateSP = CInt(dgvPresets(1, 5).Value)
flowDevAlrmSP = CInt(dgvPresets(1, 6).Value)
oilTempAlrmSP = CInt(dgvPresets(1, 7).Value)
partTempAlrmSP = CInt(dgvPresets(1, 8).Value)
exitOilTempAlrmSP = CInt(dgvPresets(1, 9).Value)
torqueAlrmSP = CSng(dgvPresets(1, 10).Value)
spindleOverrunSP = CInt(dgvNonSync(1, 0).Value)
OverrunHoldTimeSP = CInt(dgvNonSync(1, 1).Value)
actuatorOnRpmSp = CInt(dgvNonSync(1, 2).Value)
actuatorOffRpmSP = CInt(dgvNonSync(1, 3).Value)
cycleIncrRpmSP = CInt(dgvNonSync(1, 4).Value)
recordOnRpmSP = CInt(dgvNonSync(1, 5).Value)
recordOffRpmSP = CInt(dgvNonSync(1, 6).Value)
Dim bools() As Boolean = New Boolean(10) {testTypeNS, testTypeOR, torqueTypeBreak, torqueTypeFix, sheaveHigh, sheaveLow, _
directionCW, directionCCW, pneuActuateAuto, elecActuateAuto, hydrActuateAuto}
Dim Presets() As Integer = New Integer(16) {cyclesSP, oilFlowSP, oilTempSP, spindleSpeedSP, accelRateSP, decelRateSP, flowDevAlrmSP, _
oilTempAlrmSP, partTempAlrmSP, exitOilTempAlrmSP, spindleOverrunSP, OverrunHoldTimeSP, _
actuatorOnRpmSp, actuatorOffRpmSP, cycleIncrRpmSP, recordOnRpmSP, recordOffRpmSP}
Dim csv As String = "myfile.csv"
Dim i As Integer = 0
'Create data table with columns and column names
Dim sourceTable As DataTable = New DataTable()
sourceTable.Columns.AddRange(New DataColumn() {New DataColumn("BooleanValues", GetType(Boolean)), _
New DataColumn("IntValues", GetType(Integer)), _
New DataColumn("singValues", GetType(Single)), _
New DataColumn("accelVals", GetType(Integer)), _
New DataColumn("decelVals", GetType(Integer)), _
New DataColumn("speedVals", GetType(Integer)), _
New DataColumn("timeVals", GetType(Integer)), _
New DataColumn("flowVals", GetType(Integer)), _
New DataColumn("tempVals", GetType(Integer))})
'Add rows to the data table
For i = 0 To 49
sourceTable.Rows.Add()
Next
'Populate first column (called BooleanValues) of cells in the Data Table with the boolean values in the Array
i = 0
For Each [bool] In bools
sourceTable.Rows(i).Item("BooleanValues") = bools(i)
i += 1
Next
'Populate second column (called IntValues) of cells in the Data Table with the integer values n the Array
i = 0
For Each [integer] In Presets
sourceTable.Rows(i).Item("IntValues") = Presets(i)
i += 1
Next
'Populate third column (called singValues) cell in the Data Table with a single variable specified
i = 0
sourceTable.Rows(i).Item("singValues") = torqueAlrmSP
'Populate the remaining columns in the Data Table with the values that are in the DataGridView on the form
For Each r As DataGridViewRow In dgvStepTest.Rows
sourceTable.Rows(i).Item("accelVals") = dgvStepTest(0, i).Value
sourceTable.Rows(i).Item("decelVals") = dgvStepTest(1, i).Value
sourceTable.Rows(i).Item("speedVals") = dgvStepTest(2, i).Value
sourceTable.Rows(i).Item("timeVals") = dgvStepTest(3, i).Value
sourceTable.Rows(i).Item("flowVals") = dgvStepTest(4, i).Value
sourceTable.Rows(i).Item("tempVals") = dgvStepTest(5, i).Value
i += 1
Next
'Call the 'ConvertToCSV' Sub that converts the DataTable to a CSV and saves it somewhere to disk
ConvertToCSV(sourceTable, "C:\Users\dmckin01\Desktop\Recipes\test.csv", ",")
End Sub