Reputation: 53
I am trying to load a CSV to a datatable and currently with the code I am using it will only load the first two columns of data. However some lines have like 7 columns. I do not think this is formatted in the correct CSV syntax, however I believe there is enough info there to extract what I need. I am looking to get all the info pulled into a datagrid, and view it as if I were opening Excel or some program of the like so then I can access the various cells for the required info. Sample code to pull in CSV:
Private Sub ReadCSV_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ABInfo = GetCsvData("C:\epds\WIP\", "test2.csv")
Bind_Grid_ABInfo(ABInfo)
End Sub
Public Sub Bind_Grid_ABInfo(ByVal DT As DataTable)
Try
DGABInfo.DataSource = DT
DGABInfo.Refresh()
Catch ex As Exception
End Try
End Sub
Public Function GetCsvData(ByVal strFolderPath As String, ByVal strFileName As String) As DataTable
Try
Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFolderPath & ";Extended Properties=""text;HDR=yes;FMT=Delimited"";"
Dim conn As New OleDbConnection(strConnString)
Try
conn.Open()
Dim cmd As New OleDbCommand("SELECT * FROM [" & strFileName & "]", conn) '("SELECT * FROM [" & strFileName & "]", conn)
Dim da As New OleDbDataAdapter()
da.SelectCommand = cmd
Dim ds As New DataSet()
da.Fill(ds)
da.Dispose()
Return ds.Tables(0)
Catch
Return Nothing
Finally
conn.Close()
End Try
Catch ex As Exception
End Try
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
Me.Dispose()
End Sub
here is the sample csv --- I'm not sure how to keep the formatting
Process,Quote
File Name,project_10_-_freeform_cover
Project Name,Project 10 - Freeform Cover
Customer Id,1234567
Dealer,dealer
Dealer Name,name
Dealer Telephone,1234567890
Dealer Mobile,
Dealer Email,[email protected]
Dealer Address,address
Distributor Name,dist
Distributor,dist
Distributor Address,address
Distributor Telephone,1234567890
Distributor Email,[email protected]
Customer Name,name
Customer Telephone,1234567890
Customer Address,address
Description,L shape pool with slide
Comments,
Measurement Unit,inches
Cover Type,Safety
Pool Shape,Freeform
Cover Color,Blue
Cover Material,Standard Mesh
Grid Size,3 x 3
Replacement,No
Deck Type,Concrete
Deck Width,Full 3' of decking around all sides of the pool.
Coping Type,Concrete
Coping Width,12.0,1' 0"
Perimeter Padding,No
Warnings,None.
A-B Length,401.0,33' 5"
Feature,AB
Type,
Removable,No
Number of points,2
Line Length,401.0,33' 5"
Point,A,B,X,Y,Label,Type
1,,,0.0,0.0,A,Position
2,,,401.0,0.0,B,Position
Feature,Pool
Number of points,25
Calculated Length,467.1,38' 11.1"
Calculated Width,348.4,29' 0.4"
Calculated Perimeter,1386.7,115' 6.7"
Point,A,B,X,Y,Label,Type
1,375.0,61.0,371.2,53.2,,A-B
2,357.0,66.0,354.0,46.3,,A-B
3,190.0,221.0,184.6,44.9,,A-B
4,79.0,341.0,63.3,47.3,,A-B
5,52.0,400.0,4.4,51.8,,A-B
6,73.0,412.0,-4.5,72.9,,A-B
7,146.0,430.0,-3.5,146.0,,A-B
8,165.0,438.0,-4.8,164.9,,A-B
9,191.0,449.0,-5.4,190.9,,A-B
10,210.0,457.0,-4.9,209.9,,A-B
11,341.0,530.0,-4.8,341.0,,A-B
12,356.0,517.0,25.2,355.1,,A-B
13,379.0,531.0,28.0,378.0,,A-B
14,396.0,542.0,29.7,394.9,,A-B
15,401.0,520.0,63.8,395.9,,A-B
16,408.0,497.0,100.1,395.5,,A-B
17,390.0,481.0,101.7,376.5,,A-B
18,371.0,464.0,103.7,356.2,,A-B
19,372.0,442.0,129.5,348.7,,A-B
20,318.0,391.0,136.0,287.5,,A-B
21,278.0,354.0,140.6,239.8,,A-B
22,305.0,314.0,193.6,235.7,,A-B
23,436.0,234.0,369.3,231.8,,A-B
24,408.0,159.0,376.5,157.1,,A-B
25,397.0,130.0,375.9,127.6,,A-B
Feature,Slide
Type,Obstacle
Removable,No
Number of points,4
Line Length,64.0,5' 4.0"
Point,A,B,X,Y,Label,Type
1,146.0,430.0,-3.5,146.0,,A-B
2,165.0,438.0,-4.8,164.9,,A-B
3,191.0,449.0,-5.4,190.9,,A-B
4,210.0,457.0,-4.9,209.9,,A-B
Cross Pool Measurement,Cross Measurement 1
Measured Length,381.0,31' 9"
Calculated Length,381.4,31' 9.4"
Point,A,B,X,Y,Label,Type,Feature,Point
1,165.0,438.0,-4.8,164.9,8,Existing,Pool,8
2,408.0,159.0,376.5,157.1,24,Existing,Pool,24
Cross Pool Measurement,Cross Measurement 2
Measured Length,347.0,28' 11"
Calculated Length,348.6,29' 0.6"
Point,A,B,X,Y,Label,Type,Feature,Point
1,401.0,520.0,63.8,395.9,15,Existing,Pool,15
2,79.0,341.0,63.3,47.3,4,Existing,Pool,4
Upvotes: 0
Views: 119
Reputation: 2317
If you know ahead of time about the format you will be importing, which you do, then you could do something like this. According to your current data, 10 columns will be enough to host a data file. Therefore, I simply put each "data column" in a GridviewColumn.
sorry this is c#, not vb.net
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
namespace CsvToDataGridView_47289838
{
public partial class Form1 : Form
{
DataGridView dgv = new DataGridView();
BindingList<dgvEntry> dgvdata = new BindingList<dgvEntry>();
string filePath = @"M:\StackOverflowQuestionsAndAnswers\CsvToDataGridView_47289838\SampleData.txt";
public Form1()
{
InitializeComponent();
initDGV();
addData();
}
private void addData()
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
{
string currentLine = string.Empty;
while ((currentLine = sr.ReadLine()) != null)
{
dgvEntry entry = new CsvToDataGridView_47289838.dgvEntry();
List<string> splitted = currentLine.Split(',').ToList();
for (int i = 0; i < splitted.Count; i++)
{
switch (i)
{
case 0:
entry.col1 = splitted[i];
break;
case 1:
entry.col2 = splitted[i];
break;
case 2:
entry.col3 = splitted[i];
break;
case 3:
entry.col4 = splitted[i];
break;
case 4:
entry.col5 = splitted[i];
break;
case 5:
entry.col6 = splitted[i];
break;
case 6:
entry.col7 = splitted[i];
break;
case 7:
entry.col8 = splitted[i];
break;
case 8:
entry.col9 = splitted[i];
break;
case 9:
entry.col10 = splitted[i];
break;
default:
break;
}
}
dgvdata.Add(entry);
}
}
}
private void initDGV()
{
dgv.Dock = DockStyle.Fill;
this.Controls.Add(dgv);
dgv.DataSource = dgvdata;
}
}
public class dgvEntry
{
public string col1 { get; set; }
public string col2 { get; set; }
public string col3 { get; set; }
public string col4 { get; set; }
public string col5 { get; set; }
public string col6 { get; set; }
public string col7 { get; set; }
public string col8 { get; set; }
public string col9 { get; set; }
public string col10 { get; set; }
}
}
Upvotes: 1