基于从分隔文本文件派生的数据表在datagridview中显示图像

时间:2015-02-04 20:17:42

标签: vb.net datagridview datatable

新手说到数据表和datagridview。有一个分隔的文本文件,示例行如下:

;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文件,转换为数据表然后转换为datagrid视图:

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”等列重命名为我选择的标题(例如“输入控制”“控制器功能”等....

有人可以建议吗?谢谢

2 个答案:

答案 0 :(得分:1)

如果您希望DataGridViewImageColumn显示来自绑定DataTable的图像,则该表必须在绑定Byte中包含DataColumn个数组。如果您想从图片文件中获取Byte数组,则可以拨打File.ReadAllBytes

对于列标题,您必须在绑定后手动设置它们。您可以遍历网格的Columns集合并设置每列的HeaderText

答案 1 :(得分:0)

同一船上任何人的最终守则:

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