根据来自定界文本文件的数据 table 在 datagridview 中显示图像
Display images in datagridview based on data table derived from delimited text file
数据 table 和数据网格视图方面的新手。有一个带分隔符的文本文件,示例行如下:
;AnalogueJoystick_1;LStick;C:\Users\Steve\Scripts\Projects\CPWizBiz\Code\CPWizBizW\Assets\Images\System_Controller_Components\Sony PSX\LStick #4.png;Steer;LStick;
KEYCODE_A;Button_01;Triangle;;Left Airbrake;Cross;View
KEYCODE_C;Button_03;Square;C:\Users\Steve\Scripts\Projects\CPWizBiz\Code\CPWizBizW\Assets\Images\System_Controller_Components\Sony PSX\Square.png;Ditch Weapon;Square;
请注意中间 'row' 图像 'cell' 为空。
也有此代码导入 .txt 文件,转换为数据 table,然后到数据网格视图:
Private Sub LoadCSV(CSVFile As String)
Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(CSVFile)
TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(";")
Dim TextFileTable As DataTable = Nothing
Dim Column As DataColumn
Dim Row As DataRow
Dim UpperBound As Int32
Dim ColumnCount As Int32
Dim CurrentRow As String()
While Not TextFileReader.EndOfData
Try
CurrentRow = TextFileReader.ReadFields()
If Not CurrentRow Is Nothing Then
''# Check if DataTable has been created
If TextFileTable Is Nothing Then
TextFileTable = New DataTable("TextFileTable")
''# Get number of columns
UpperBound = CurrentRow.GetUpperBound(0)
''# Create new DataTable
For ColumnCount = 0 To UpperBound
Column = New DataColumn()
Column.DataType = System.Type.GetType("System.String")
Column.ColumnName = "Column" & ColumnCount
Column.Caption = "Column" & ColumnCount
Column.ReadOnly = True
Column.Unique = False
TextFileTable.Columns.Add(Column)
Next
End If
Row = TextFileTable.NewRow
For ColumnCount = 0 To UpperBound
Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
Next
TextFileTable.Rows.Add(Row)
End If
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
TextFileReader.Dispose()
DataGridView1.DataSource = TextFileTable
End Sub
我正在努力使第 3 列(图像文件列)在 datagridview 中显示为实际图像。此外,需要能够处理空值。
其次,我希望能够将列从 "Column0" "coulmn1" 等重命名为我选择的 headers(例如 "Input Control" "Controller Function"等....
有人可以指教吗?谢谢
如果您希望 DataGridViewImageColumn
显示来自绑定 DataTable
的图像,那么 table 必须在绑定 DataColumn
中包含 Byte
数组。如果您想从图像文件中获取 Byte
数组,则可以调用 File.ReadAllBytes
.
至于headers列,您必须在绑定后手动设置。可以循环遍历grid的Columns
collection,设置每列的HeaderText
同船之人的最终代码:
Private Sub LoadCSV(CSVFile As String)
Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(CSVFile)
TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(";")
Dim TextFileTable As DataTable = Nothing
Dim Column As DataColumn
Dim Row As DataRow
Dim UpperBound As Int32
Dim ColumnCount As Int32
Dim CurrentRow As String()
While Not TextFileReader.EndOfData
Try
CurrentRow = TextFileReader.ReadFields()
If Not CurrentRow Is Nothing Then
''# Check if DataTable has been created
If TextFileTable Is Nothing Then
TextFileTable = New DataTable("TextFileTable")
''# Get number of columns
UpperBound = CurrentRow.GetUpperBound(0)
''# Create new DataTable
For ColumnCount = 0 To UpperBound
If ColumnCount = 3 Then
Column = New DataColumn()
Column.DataType = System.Type.GetType("System.Byte[]")
Column.ColumnName = "Column" & ColumnCount
Column.Caption = "Column" & ColumnCount
Column.ReadOnly = True
Column.Unique = False
TextFileTable.Columns.Add(Column)
Else
Column = New DataColumn()
Column.DataType = System.Type.GetType("System.String")
Column.ColumnName = "Column" & ColumnCount
Column.Caption = "Column" & ColumnCount
Column.ReadOnly = True
Column.Unique = False
TextFileTable.Columns.Add(Column)
End If
Next
End If
Row = TextFileTable.NewRow
For ColumnCount = 0 To UpperBound
If ColumnCount = 3 Then
If CurrentRow(ColumnCount).ToString <> Nothing Then
Debug.WriteLine("Img Path: [" & CurrentRow(ColumnCount).ToString & "]")
Dim img = Image.FromFile(CurrentRow(ColumnCount).ToString)
Dim ms = New MemoryStream()
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Row("Column" & ColumnCount) = ms.ToArray
End If
Debug.WriteLine("Img Null")
Else
Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
End If
Next
TextFileTable.Rows.Add(Row)
End If
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
TextFileTable.Columns(0).ColumnName = "Input Code"
TextFileTable.Columns(1).ColumnName = "Rig Control"
TextFileTable.Columns(2).ColumnName = "Final Controller Functions"
TextFileTable.Columns(3).ColumnName = "Image"
TextFileTable.Columns(4).ColumnName = "Final Game Function"
TextFileTable.Columns(5).ColumnName = "Default Controller"
TextFileTable.Columns(6).ColumnName = "Default Game Functions"
TextFileTable.Columns(7).ColumnName = "Type"
TextFileReader.Dispose()
DataGridView1.DataSource = TextFileTable
End Sub
数据 table 和数据网格视图方面的新手。有一个带分隔符的文本文件,示例行如下:
;AnalogueJoystick_1;LStick;C:\Users\Steve\Scripts\Projects\CPWizBiz\Code\CPWizBizW\Assets\Images\System_Controller_Components\Sony PSX\LStick #4.png;Steer;LStick;
KEYCODE_A;Button_01;Triangle;;Left Airbrake;Cross;View
KEYCODE_C;Button_03;Square;C:\Users\Steve\Scripts\Projects\CPWizBiz\Code\CPWizBizW\Assets\Images\System_Controller_Components\Sony PSX\Square.png;Ditch Weapon;Square;
请注意中间 'row' 图像 'cell' 为空。
也有此代码导入 .txt 文件,转换为数据 table,然后到数据网格视图:
Private Sub LoadCSV(CSVFile As String)
Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(CSVFile)
TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(";")
Dim TextFileTable As DataTable = Nothing
Dim Column As DataColumn
Dim Row As DataRow
Dim UpperBound As Int32
Dim ColumnCount As Int32
Dim CurrentRow As String()
While Not TextFileReader.EndOfData
Try
CurrentRow = TextFileReader.ReadFields()
If Not CurrentRow Is Nothing Then
''# Check if DataTable has been created
If TextFileTable Is Nothing Then
TextFileTable = New DataTable("TextFileTable")
''# Get number of columns
UpperBound = CurrentRow.GetUpperBound(0)
''# Create new DataTable
For ColumnCount = 0 To UpperBound
Column = New DataColumn()
Column.DataType = System.Type.GetType("System.String")
Column.ColumnName = "Column" & ColumnCount
Column.Caption = "Column" & ColumnCount
Column.ReadOnly = True
Column.Unique = False
TextFileTable.Columns.Add(Column)
Next
End If
Row = TextFileTable.NewRow
For ColumnCount = 0 To UpperBound
Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
Next
TextFileTable.Rows.Add(Row)
End If
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
TextFileReader.Dispose()
DataGridView1.DataSource = TextFileTable
End Sub
我正在努力使第 3 列(图像文件列)在 datagridview 中显示为实际图像。此外,需要能够处理空值。
其次,我希望能够将列从 "Column0" "coulmn1" 等重命名为我选择的 headers(例如 "Input Control" "Controller Function"等....
有人可以指教吗?谢谢
如果您希望 DataGridViewImageColumn
显示来自绑定 DataTable
的图像,那么 table 必须在绑定 DataColumn
中包含 Byte
数组。如果您想从图像文件中获取 Byte
数组,则可以调用 File.ReadAllBytes
.
至于headers列,您必须在绑定后手动设置。可以循环遍历grid的Columns
collection,设置每列的HeaderText
同船之人的最终代码:
Private Sub LoadCSV(CSVFile As String)
Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(CSVFile)
TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(";")
Dim TextFileTable As DataTable = Nothing
Dim Column As DataColumn
Dim Row As DataRow
Dim UpperBound As Int32
Dim ColumnCount As Int32
Dim CurrentRow As String()
While Not TextFileReader.EndOfData
Try
CurrentRow = TextFileReader.ReadFields()
If Not CurrentRow Is Nothing Then
''# Check if DataTable has been created
If TextFileTable Is Nothing Then
TextFileTable = New DataTable("TextFileTable")
''# Get number of columns
UpperBound = CurrentRow.GetUpperBound(0)
''# Create new DataTable
For ColumnCount = 0 To UpperBound
If ColumnCount = 3 Then
Column = New DataColumn()
Column.DataType = System.Type.GetType("System.Byte[]")
Column.ColumnName = "Column" & ColumnCount
Column.Caption = "Column" & ColumnCount
Column.ReadOnly = True
Column.Unique = False
TextFileTable.Columns.Add(Column)
Else
Column = New DataColumn()
Column.DataType = System.Type.GetType("System.String")
Column.ColumnName = "Column" & ColumnCount
Column.Caption = "Column" & ColumnCount
Column.ReadOnly = True
Column.Unique = False
TextFileTable.Columns.Add(Column)
End If
Next
End If
Row = TextFileTable.NewRow
For ColumnCount = 0 To UpperBound
If ColumnCount = 3 Then
If CurrentRow(ColumnCount).ToString <> Nothing Then
Debug.WriteLine("Img Path: [" & CurrentRow(ColumnCount).ToString & "]")
Dim img = Image.FromFile(CurrentRow(ColumnCount).ToString)
Dim ms = New MemoryStream()
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Row("Column" & ColumnCount) = ms.ToArray
End If
Debug.WriteLine("Img Null")
Else
Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
End If
Next
TextFileTable.Rows.Add(Row)
End If
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
TextFileTable.Columns(0).ColumnName = "Input Code"
TextFileTable.Columns(1).ColumnName = "Rig Control"
TextFileTable.Columns(2).ColumnName = "Final Controller Functions"
TextFileTable.Columns(3).ColumnName = "Image"
TextFileTable.Columns(4).ColumnName = "Final Game Function"
TextFileTable.Columns(5).ColumnName = "Default Controller"
TextFileTable.Columns(6).ColumnName = "Default Game Functions"
TextFileTable.Columns(7).ColumnName = "Type"
TextFileReader.Dispose()
DataGridView1.DataSource = TextFileTable
End Sub