 |
|
 |
I have a txt file with the data in 10 character fields, but no comma btwn them, this way:
V2bbbbbbbbV4bbbbbbbb221.8370bb299.9110bb83.150bbbb1.000bbbbb1.154bbbbbbbb
V2bbbbbbbbV1bbbbbbbb79.4033bbb99.9483bbb64.215bbbb1.000bbbbb1.154bbbbbbbb
(b = blank)
How can I add these data to a Datagridview?
When I load the data everything goes to the first column.
If I use
Do While objReader.Peek() <> -1
TextLine = objReader.ReadLine()
SplitLine = Split(TextLine, " ")
Me.dgv1.Rows.Add(SplitLine)
Loop
each record breaks after the first field.
Thank you for your help
modified on Friday, April 22, 2011 7:46 AM
|
|
|
|
 |
|
 |
It works grest in a simples datagridview but when
I try to import data into a bound database
and it doesn't work.
How can i do that?
Thanks
|
|
|
|
 |
|
|
 |
|
 |
Very useful code, allowed me to change the comma values from a file to tab.
Thanks very much for this.
|
|
|
|
 |
|
 |
but
1. When I save the data into a file, these data are added at the end of the file leaving a blank row with a comma between previous data and the recently saved, and the csv grows.
2. If I start with no data to load and try to save the first file I get the message "... Access to the path 'C:\dummy.csv' denied ..."
How can we fix this?
Thanx
|
|
|
|
 |
|
 |
Hi,
Try changing the parameter in the line "Dim objWriter As New System.IO.StreamWriter(fName, True)"
For your second problem, I dont know why that is happening without seeing what you are doing. Try the above change and see if it helps.
Cheers
Think, and it can be done
|
|
|
|
 |
|
 |
Changing "Append" to False, works. Thank you.
What I am doing is saving without loading first. It works depending on the directory I choose.
For instance:
1. It doesn't write on c:\ or on c:\Users\
2. It does write on C:\Users\FGL\Documents\ or C:\Users\FGL\
so I think I need to use as default value for the path-textbox a valid folder, which I know for my computer but don't for another user's one.
Thank you.
|
|
|
|
 |
|
 |
I think I got it!
I'm using the SaveFileDialog to save the file and it works great!
Thank you again!
|
|
|
|
 |
|
 |
I am using your code for importing xls to datagridview. But
I am not getting columns right, Getting encoded data...
Cant see my actual data, I can show you screen shot.. If you give me ur email.
Thanks
|
|
|
|
 |
|
 |
Hi,
This code works for "flat files" (.txt, .csv), .xml files are "not flat files" and so this code will not work.
Try to open .xml files in Notepad and you will see what I mean by "not flat file"
CheersThink, and it can be done
|
|
|
|
 |
|
 |
Had to add:
Dim OpenFileDialog1 As New OpenFileDialog
before:
OpenFileDialog1.InitialDirectory = "C:\TEMP\"
But after that works wonderfully.
|
|
|
|
 |
|
 |
hi guys
am new in programming especialy in vb. I am supposed to validate some of the Cells in datagridview.
I will like to know what is a validation?
also the editing capabilities of Datagridview Control
Please help me gyz
Amani
|
|
|
|
 |
|
 |
Kudos and Karma to you!
I had been struggling with the problem you addressed so clearly and cleanly in this article.
Thank you very much!!!
Best,
Filemon
|
|
|
|
 |
|
 |
I am happy that this helped you in your project.
Think, and it can be done
|
|
|
|
 |
|
 |
I did notice however that you didnt close the reader after the text file was read into the DGV.
This meant that you could not save the file under the same name.
I added objReader.Close() between "loop" and "else" statements on Button1 and it resolved this immediatly.
Also I added Me.DataGridView1.Rows.Clear() to make sure the DGV was clear before any text was loaded into it.
Well done and thanks for posting
Baillie11
|
|
|
|
 |
|
 |
You are right for the objReader.Close() and thanks for pointing it out.
I am glad this helped you in your project.
Think, and it can be done
|
|
|
|
 |
|
 |
Ok so I tried your code thanks for posting it I just Have one Question How did you add rows to the Datagridview when you have no columns? That is the First Error that is Caught when you run your Code do you have a fix for that?
Thanks
|
|
|
|
 |
|
 |
I think, you need to create the columns manually before actually running this code.
|
|
|
|
 |
|
 |
It is really nice article...But I had to add a condition to remove the extra comma that appears
at the end of each line while exporting the data from datagridview1 to CSV file
Something like that
If I = DataGridView1.Columns.Count - 1 Then
rowLine = rowLine + cellvalue
Else
rowLine = rowLine + cellvalue + ","
End If
Thanks
Marwa Elbadry
|
|
|
|
 |
|
 |
removing comma at the end makes the *.csv file look neat but....
Is it really required to remove the ending comma ","???
Think, and it can be done
|
|
|
|
 |
|
 |
http://forums.devx.com/showthread.php?t=148498[^]
I recently used this method, thought you might be interested. fyi. Mike
temp = System.Windows.Forms.Application.StartupPath
temp = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CStr(temp) & ";Extended Properties=Text;"
Dim sConnectionString As String = CStr(temp) '"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\mswayze\My Documents\Visual Studio Projects\winapp2\WindowsApplication1;Extended Properties=Text;"
Dim objConn As New OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDbCommand("SELECT * FROM PRICE.csv", objConn)
Dim objAdapter1 As New OleDbDataAdapter
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet
objAdapter1.Fill(objDataset1, "PRICE")
DataGrid1.DataSource = objDataset1.Tables("PRICE").DefaultView
objConn.Close()
and then I set the datagrid column widths with the following--
Try
Dim aGridTableStyle As New DataGridTableStyle
aGridTableStyle.MappingName = "PRICE"
DataGrid1.TableStyles.Add(aGridTableStyle)
Dim g As Graphics = DataGrid1.CreateGraphics()
Dim OFFSET As Integer = Convert.ToInt32(Math.Ceiling(g.MeasureString(" ", DataGrid1.Font).Width))
DataGrid1.TableStyles("PRICE").GridColumnStyles("TYPE").Width = 420 + OFFSET
DataGrid1.TableStyles("PRICE").GridColumnStyles(4).Width = 420 + OFFSET
Catch ex As Exception
Exit Try
End Try
|
|
|
|
 |
|
 |
This method, however, leads to impredictable results.
In example, it transforms the DataType of each DataColumn in the DataTable to a type it decides by itself...
I mean that it could transform a DataColumn (let's say it's called CurrencyValue) to Int32, just because the first line contains 22.
Then, the second line contains 33,11.... and you get 33, because the DataColumn's DataType is Int32!!
And the DataColumns DataTypes aren't changeable, once the columns are filled.
Anyway, the values have been changed, so they are false values.
Not even to mention if you deal with dates.
I'd advice you keep the String DataType for each DataColumn.
I had to do triple backwards deathly jumps to solve this problem:
Load the DataTable once (the values are now unusable), prepare another DataTable using the column names from the first DataTable, but FORCE the DataType for each DataColumn to String.
Load the Values using a StreamReader.
Now I got It working.
My file was an "EXCEL CSV", so the values are separated by semicolons (;).
Still I needed to convert commas to dots, since I wanted to insert these values into a db.
Well, inpractice, I did it while reading from the file, correctly populating the second DataTable at once.
Hope this reflections will help anybody facing the same problems.
Luca Crisi, MCP
|
|
|
|
 |