first to me: I'm fairly new in vb.net and still a beginner and reading many many guides/articles to improve myself but sadly I can't get over this problem.
I have a Form with data gridview and set the Columns in my Text:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
table.Columns.Add("ID", Type.GetType("System.String"))
table.Columns.Add("First Name", Type.GetType("System.String"))
table.Columns.Add("Last Name", Type.GetType("System.String"))
table.Columns.Add("Age", Type.GetType("System.String"))
DataGridView1.DataSource = table
End Sub
Now I want to import the text from a Text.File which I already created: (it was created with the exact method like this one but not imported it was exported) The text file looks like this:
1|a|a|a
2|b|b|b
3|c|c|c
To import the Textfile:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines() As String
Dim vals() As String
lines = File.ReadAllLines("C:\Users\duidi\Desktop\Test\log3.txt")
For i As Integer = 0 To lines.Length - 1 Step +1
vals = lines(i).ToString().Split("|")
Dim row(vals.Length - 1) As String
For j As Integer = 0 To vals.Length - 1 Step +1
row(j) = vals(j).Trim()
Next j
table.Rows.Add(row)
Next i
End Sub
To my Problems:
I want to change the width of the columns (maybe something else too like font, height) but I have absolutely no clue how to edit the width of the columns while they are in a datatable.
I could add the columns via Form.Designer and change the width there but than I have no clue how to get the connections between datatable and columns.
What is the actual purpose of this program:
I want to export the data from a dataviewgrid (maybe encrypt it (later point)) than import the data in the exact same datagridview edit it and export again. And so on.
I know there are many, many posts out there which describe the import/export to .txt file but I am still too new to adapt it on my own to my program, I need to see the solution on my own text. I could really need your help!
Sorry for my bad English I am not a native
Here is my full (functional) code
Imports System.IO
Public Class Form1
Dim table As New DataTable("Table")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
table.Columns.Add("ID", Type.GetType("System.String"))
table.Columns.Add("First Name", Type.GetType("System.String"))
table.Columns.Add("Last Name", Type.GetType("System.String"))
table.Columns.Add("Age", Type.GetType("System.String"))
DataGridView1.DataSource = table
End Sub
Private Sub btn_export_Click(sender As Object, e As EventArgs) Handles btn_export.Click
Dim writer As TextWriter = New StreamWriter("C:\Users\duidi\Desktop\Test\log.txt")
For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
If j = DataGridView1.Columns.Count - 1 Then
writer.Write(DataGridView1.Rows(i).Cells(j).Value.ToString())
Else
writer.Write(DataGridView1.Rows(i).Cells(j).Value.ToString() & "|")
End If
Next j
writer.WriteLine("")
Next i
writer.Close()
MessageBox.Show("export fertig")
End Sub
Private Sub btn_import_Click(sender As Object, e As EventArgs) Handles btn_import.Click
Dim lines() As String
Dim vals() As String
table.Clear()
lines = File.ReadAllLines("C:\Users\duidi\Desktop\Test\log.txt")
For i As Integer = 0 To lines.Length - 1 Step +1
vals = lines(i).ToString().Split("|")
Dim row(vals.Length - 1) As String
For j As Integer = 0 To vals.Length - 1 Step +1
row(j) = vals(j).Trim()
Next j
table.Rows.Add(row)
Next i
End Sub
End Class
Access to columns and styles:
Dim columnIndex = 0
Dim column = DataGridView1.Columns(columnIndex)
column.Width = 100
column.HeaderCell.Style = New DataGridViewCellStyle With {
.Font = New Font("Times New Roman", 20, FontStyle.Bold),
.Alignment = DataGridViewContentAlignment.MiddleCenter
}
column.DefaultCellStyle = New DataGridViewCellStyle With {
.Font = New Font("Arial", 8, FontStyle.Italic),
.Alignment = DataGridViewContentAlignment.MiddleRight
}
Here is the style documentation
What are your import problems? It is not clear from the question.
What is table
object?