5 Replies - 631 Views - Last Post: 19 September 2015 - 10:15 AM Rate Topic: -----

#1 kim_sunwoo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 15

Import csv data into datagridview identifying double values

Posted 19 September 2015 - 04:44 AM

Hello everyone!

I'm new to VB.net and I'm trying to import a .csv separated by ";" into a DataGridView.
I want the DataGridView to just show this data, so it can't be edited in any way by the user.
The code I'm using is this:

Private Sub ReadFile()
        Dim sr As New StreamReader("resource/database.txt")
        Dim strLineIn As String
        Dim strSplit() As String
        DataGridView1.Rows.Clear()
        Do While sr.Peek <> -1
            strLineIn = sr.ReadLine
            strSplit = strLineIn.Split(";")
            DataGridView1.Rows.Add(strSplit(0), strSplit(1), strSplit(2), strSplit(3))
        Loop
        sr.Close()
        DataGridView1.Columns(3).ValueType = GetType(Decimal)
    End Sub



This works perfectly fine, as I have 4 columns defined in the datagridview and all information is shown without any issue.

The problem is that one of those columns has currency value (example 24,00), and since all lines are read as a string, when that column is sorted it does so in alphabetic order (example 105, 23, 5, instead of 5, 23, 105). I've spent hours and hours reading in internet possible solutions and found nothing that I could use.

Can you help me with it?

Is This A Good Question/Topic? 0
  • +

Replies To: Import csv data into datagridview identifying double values

#2 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6776
  • View blog
  • Posts: 27,943
  • Joined: 12-December 12

Re: Import csv data into datagridview identifying double values

Posted 19 September 2015 - 05:14 AM

I would build a small test csv that uses . as decimal separator. If this works then it is the comma separator that is the issue. Then it is a culture issue and I would start by investigating DataGridViewCellStyle.FormatProvider.

But perhaps the first tests can be via the Windows culture (regional) settings.
Was This Post Helpful? 1
  • +
  • -

#3 kim_sunwoo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 15

Re: Import csv data into datagridview identifying double values

Posted 19 September 2015 - 05:17 AM

Hi Andrew,

I've also tested the csv by not using decimals and just writting integer numers, so the "," or "." as decimal separator is not the issue.
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1535
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: Import csv data into datagridview identifying double values

Posted 19 September 2015 - 09:24 AM

If you set the Column`s Type before reading the file and convert the last string in the split string to a Decimal Type when adding it to the DGV it seems to work alright on my end.

However, i am using a decimal point in my numbers because that is what would be used on my machine, not a comma. It may work alight using a comma if it is run on a computer that the culture uses commas instead of decimal points, i`m not sure.
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.Columns(3).ValueType = GetType(Decimal) 'set the column type before loading the file
        ReadFile()
    End Sub

    Private Sub ReadFile()
        Dim sr As New StreamReader("C:\testfolder\database.txt")
        Dim strLineIn As String
        Dim strSplit() As String
        DataGridView1.Rows.Clear()
        Do While sr.Peek <> -1
            strLineIn = sr.ReadLine
            strSplit = strLineIn.Split(";"c)
            DataGridView1.Rows.Add(strSplit(0), strSplit(1), strSplit(2), CDec(strSplit(3))) 'convert the last string to a decimal type
        Loop
        sr.Close()
    End Sub

End Class


This post has been edited by IronRazer: 19 September 2015 - 09:25 AM

Was This Post Helpful? 1
  • +
  • -

#5 kim_sunwoo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 15

Re: Import csv data into datagridview identifying double values

Posted 19 September 2015 - 10:04 AM

Hello Iron Razer,

Don't know how to thank you for such a simple and effective solution.
It now works perfectly fine. You made my day, as I've been stuck in this piece of code for three days.

Just for the sake of curiosity, in my computer it takes comma as the proper separation for decimals instead of the dot.

This post has been edited by andrewsw: 19 September 2015 - 10:24 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1535
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: Import csv data into datagridview identifying double values

Posted 19 September 2015 - 10:15 AM

You`re welcome, i`m glad it helped. 8)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1