DataGridView Format

spyderman4g63

New member
Joined
Oct 4, 2006
Messages
3
Programming Experience
Beginner
I am new to vb and writing a program in vb.net 2005 in which I convert a fixed width file to csv then read it into a datagridview.

When I convert the data it works fine.
Sample:

000P014 000-11034-42005 DRY VERMOUTH 05 00 REG
20309 000-12000-00017 PEPSI 24 PAK 04

converts to:

ItemNumber,UPC,Wharehouse,Description,Department,Sequence,Type,Empty
000P014,000-11034-42005,,DRY VERMOUTH,05,00,REG,
20309,000-12000-00017,,PEPSI 24 PAK,04,,,

But when I read this data into the datagridview it looks like this.
Sample:

000-11034-42005 DRY VERMOUTH 5 0 REG
20309 000-12000-00017 PEPSI 24 PAK 4

for some reason any data in the "item number" field that starts with a 0 is skipped and anything in the sequence and department fields the leading zero's are left off.

Here is the code I am using to read the csv file into the datagrid:

VB.NET:
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Duplicates\;Extended Properties=Text;"
Dim objConn As New OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDbCommand("SELECT * FROM test.csv", objConn)
Dim objAdapter1 As New OleDbDataAdapter
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet
objAdapter1.Fill(objDataset1, "test")
DataGridView1.DataSource = objDataset1.Tables(0).DefaultView
objConn.Close()

Does anyone know why the Item Number (example: 000P014) fields to be written to the data grid and/or how to have the data grid display the leading zeros on the other fields?

* sorry for the formatting. I tryed to use the <pre> tag with no luck.
 
Last edited by a moderator:
WHy go through the 2 stage process of fixed->csv->datatable

If you have code to generate the CSV, why not just use it at that point to fill a datatable


Create a new dataset object in the designer, add a datatable and give the columns proper types. Read your fixed width file in and populate a datatable at that time. I'll attach an example skeleton project to highlight what i mean, in a short while..
 
Here we have an example project.

It has 1 Form, and 1 DataSet
The dataset has 1 table representing the file and 1 table for lines that didnt import properly
The datatable columns are all declared as strings, but you can use numbers etc if you need to format them
The dataset has knowledge of how to read the file off disk
The Form has a datagridview, bound to a suitable instance of the datatable
This project should be considered pseudocode; it has never been compiled or run. The logical premise behind its operations is sound.
I can assist you further if you encounter difficulties in using this project (as a base for imagination and idea where to go with it)
 

Attachments

  • spyderman4g63_1.zip
    39.1 KB · Views: 16
Back
Top