我有一个DataGridView,我想填充双打值。当值为字符串时,DataGridView填充正常,但现在它在第一列中输出System.Double[]
而在任何其他列中都没有输出。我知道转换正在运行,因为我可以将双值打印到控制台。如何以编程方式格式化所有列? (我不想使用设计师)
我的代码:
Dim cols() as String = {"col1", "col2", "col3"} ' this goes on for 33 cols
Dim currentRow As String()
Dim row() As Double
Dim c As Integer
DataGridView1.Rows.Clear()
DataGridView1.ReadOnly = True
DataGridView1.DefaultCellStyle.Format = "G"
DataGridView1.ColumnCount = 33
For c = 0 To (cols.Length - 1)
DataGridView1.Columns(c).Name = cols(c)
Next
While Not at EndOfFile ' This line and the next are semi-pseudocode, but it's the same idea
read/parse line into currentRow
Dim x As Integer
For x = 0 To (currentRow.Length - 1)
ReDim Preserve row(x)
Double.TryParse(currentRow(x), row(x))
Next
With Me.DataGridView1.Rows
.Add(row)
End With
输出屏幕截图:
答案 0 :(得分:1)
您可以先将行添加到网格中,然后遍历数组以将信息添加到每个单元格。
Dim i as integer = DataGridView1.Rows.Add
For x As Integer = 0 to row.Count - 1
DataGridView1.Rows(i).Cells(x).Value = row(x)
Next