这实际上是很容易的。将数据表转换为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
是否有DataTable超出容器的数据的某些原因?我想知道你为什么选择这个。 – Plutonix
没有特别的理由。如果有更好的方法,更简单的方法来做到这一点,那么我愿意提供建议。我承认,我不是一个优秀的程序员。也许我会让它变得比它所需要的更加困难/复杂? – busarider29