Pokud se inspirujete dle uvedeného linku v závěru předchozího příspěvku. Vzorek obsahu souboru Seznam.txt :
Číslo;Název;Popis
1;Položka1;Popis1
2;Položka2;Popis2
3;Položka3;Popis3
4;Položka4;Popis4
Formulář Form1 obsahuje DataGridView1, ComboBox1 a Label1 :
Public Class Form1
Dim file_name As String = "C:\Seznam.txt" 'opravte si
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim dt As DataTable = TextToTable(file_name, ";", True)
DataGridView1.DataSource = dt
With ComboBox1
.DisplayMember = "Název"
.ValueMember = "Číslo"
.DataSource = New DataView(dt)
End With
With Label1
.DataBindings.Add("text", ComboBox1.DataSource, "Popis")
End With
End Sub
Private Function TextToTable(ByVal path As String, ByVal delimitter As Char, ByVal header As Boolean) As DataTable
Dim source As String = String.Empty
Dim dt As DataTable = New DataTable
If IO.File.Exists(path) Then
source = IO.File.ReadAllText(path, System.Text.Encoding.GetEncoding("windows-1250"))
Else
Throw New IO.FileNotFoundException("Could not find the file at " & path, path)
End If
Dim rows() As String = source.Split({Environment.NewLine}, StringSplitOptions.None)
For i As Integer = 0 To rows(0).Split(delimitter).Length - 1
Dim column As String = rows(0).Split(delimitter)(i)
dt.Columns.Add(If(header, column, "column" & i + 1))
Next
For i As Integer = If(header, 1, 0) To rows.Length - 1
Dim dr As DataRow = dt.NewRow
For x As Integer = 0 To rows(i).Split(delimitter).Length - 1
If x <= dt.Columns.Count - 1 Then
dr(x) = rows(i).Split(delimitter)(x)
Else
Throw New Exception("The number of columns on row " & i + If(header, 0, 1) & " is greater than the amount of columns in the " & If(header, "header.", "first row."))
End If
Next
dt.Rows.Add(dr)
Next
Return dt
End Function
End Class
Vůbec to nevypadá složitě, že? Mohlo by to takhle nějak posloužit vašemu záměru?
|