Click here to Skip to main content
14,984,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

So.. Please help :D I've been trying for two days, but with no result.
I have the DataGridView with the ComboBox as first column. It looks like this:

(combobox) John 000000
(combobox) Billy 111111

The values are upload by the button from text file, with "," behind every value, like .csv file. The combobox values are No contact, Busy. I set the DataGridView selection mode to row. So, i don't need to select every cell, i just need the whole row. Now i'm trying to do, that after click on for e.g.John, the whole row is saved to text file - it should looks like this:

Next, after click on Billy, text file looks like:

Where value 1 is "No Contact", and 2 is "Busy".
Then i want to make the upload button for read the values from text file what i saved, and put this into DataGridView. e.g:
Text file:

Click upload button: DataGridView
2,John,000000,...,...,... > Busy,John,000000,...,...
1,Billy,111111,...,... > No Contact,Billy,111111,...,...

This is what i already have:
Public Class Start
    Dim dataTable As New DataTable
    Dim cmenu As New DataGridViewComboBoxColumn()

    Private Sub IconButton2_Click(sender As Object, e As EventArgs) Handles IconButton2.Click
        Dim thereader As New IO.StreamReader("HEHE\Desktop\data\data.txt", System.Text.Encoding.Default)
        Dim sline As String = ""
            sline = thereader.ReadLine
            If sline Is Nothing Then Exit Do
            Dim thecolumns() As String = sline.Split(",")
            Dim newrow As DataRow = dataTable.NewRow
            newrow("Name") = thecolumns(0)
            newrow("LastName") = thecolumns(1)
            newrow("Number") = thecolumns(2)
            newrow("Address") = thecolumns(3)
            newrow("Age") = thecolumns(4)
            newrow("E-mail") = thecolumns(5)
        clients.DataSource = dataTable
        clients.Columns(3).CellTemplate.Style.ForeColor = RGBColors.color7
        clients.Columns(3).CellTemplate.Style.Font = New Font(clients.DefaultCellStyle.Font, FontStyle.Bold)
        clients.Columns("Address").Width = 200
        clients.Columns("E-mail").Width = 200
        clients.Columns("Age").Width = 70
        clients.Columns("Name").ReadOnly = True
        clients.Columns("LastName").ReadOnly = True
        clients.Columns("Number").ReadOnly = True
        clients.Columns("Address").ReadOnly = True
        clients.Columns("Age").ReadOnly = True
        clients.Columns("E-mail").ReadOnly = True
        Me.Text = dataTable.Rows.Count & "rows"
        IconButton2.Enabled = False
    End Sub

    Private Sub Start_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With dataTable
            .Columns.Add("Name", System.Type.GetType("System.String"))
            .Columns.Add("LastName", System.Type.GetType("System.String"))
            .Columns.Add("Number", System.Type.GetType("System.Decimal"))
            .Columns.Add("Address", System.Type.GetType("System.String"))
            .Columns.Add("Age", System.Type.GetType("System.Decimal"))
            .Columns.Add("E-mail", System.Type.GetType("System.String"))
        End With

        clients.BackgroundColor = RGBColors.color4
        cmenu.HeaderText = "Status"
        cmenu.Name = "cmb"
        cmenu.MaxDropDownItems = 4
        cmenu.Items.Add("Brak kontaktu")
        cmenu.ReadOnly = False
    End Sub

End Class

What I have tried:

Dim swWriter As New IO.StreamWriter("HEHE\Desktop\data\data.txt")
Dim LineToWrite As String = String.Empty

    For _Row As Integer = 0 To clients.Rows.Count - 1
        LineToWrite = String.Empty
        For _Column As Integer = 0 To clients.Columns.Count - 1
            LineToWrite &= "," & clients.Rows(_Row).Cells(_Column).Value.ToString
        LineToWrite = LineToWrite.Remove(0, 1)
    MessageBox.Show("Data Saved Successfully")
Catch ex As Exception
Updated 22-Aug-20 8:40am

1 solution

You can either do it your self - and that can get complicated because there is nothing stopping your users entering strings with commas in, so you need to process inputs to make sure you "escape" commas or double quote strings - or you could use a package to help you.

For CSV data, either try this: A Fast CSV Reader[^] or CsvHelper[^] - I've just started with the second one, and it's looking pretty good.
Either one will do all the "heavy lifting" for you.

In addition, if you aren't fixed on using the CSV format you show, XML and JSON are much better solutions, and are natively supported by .NET (but Newtonsoft.JSON is much better!)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900