Today we will see Fetch Text file Data into Datagridview in VB.net
Text file Data Format
111;222
333;444
555;666
777;888
Code:
Text file Data Format
111;222
333;444
555;666
777;888
Code:
private Sub DispaydatafromTextFile(txtFilePath As String)
'1st you must create columns to dgv, like:
dataGridView1.Columns.Add("col1", "Column1")
dataGridView1.Columns.Add("col2", "Column2")
'and so on...
Using sr As New StreamReader(txtFilePath)
Dim row As Integer = 0
Dim line As String
While (InlineAssignHelper(line, sr.ReadLine())) IsNot Nothing
Dim columns As String() = line.Split(";"C)
dataGridView1.Rows.Add()
For i As Integer = 0 To columns.Length - 1
dataGridView1(i, row).Value = columns(i)
Next
row += 1
End While
End Using
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
DispaydatafromTextFile("C:\Test.txt")
textBox1.Text = Convert.ToString(dataGridView1.CurrentRow.Cells(0).Value)
textBox2.Text = Convert.ToString(dataGridView1.CurrentRow.Cells(1).Value)
End Sub
Private Sub dataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs)
textBox1.Text = Convert.ToString(dataGridView1.Rows(e.RowIndex).Cells(0).Value)
textBox2.Text = Convert.ToString(dataGridView1.Rows(e.RowIndex).Cells(1).Value)
End Sub