Adding a datarow by dynamically reading a text file - vb.net

I have a program that reads a text file and creates a datatable dynamically based on the columns in the file.
This seems to work fine.
I then try to read the rest of the text file, creating a data row to add to my table. I'm using the code below but it's not working for me.
view plaincopy to clipboardprint?
Dim strFieldData As New ArrayList
Dim obj As New Object()
'this code give me 12 separate elements
strFieldData = SplitDelimitedLine(strLineOfFile, ",", "|")
Dim dr As DataRow
For i As Integer = 0 To strFieldData.Count - 1
dr = DataForSQLTable.NewRow()
dr.ItemArray(i) = strFieldData.Item(i)
Next
'however when I look at the value of dr.itemarray I see System.DBNull for all the fields
DataForSQLTable.Rows.Add(dr)
I'm obviously missing the trick. Can anyone help me please?

It looks like you're only adding the last item. You need to move the addition line inside the for loop.
For i As Integer = 0 To strFieldData.Count - 1
Dim dr as DataRow = DataForSQLTable.NewRow()
dr.ItemArray(i) = strFieldData.Item(i)
DataForSQLTable.Rows.Add(dr)
Next

Related

partial .txt file to datagridview vb.net

i have a user interface form that let's you upload a text file to a datagridview as follows
Sub Datagrid()
Dim sw = System.Diagnostics.Stopwatch.StartNew()
Using stream As System.IO.FileStream = System.IO.File.OpenRead(TextBox1.Text)
Using reader As New System.IO.StreamReader(stream)
Dim line As String = reader.ReadLine()
While (line IsNot Nothing)
Dim columns = line.Split(";")
line = reader.ReadLine()
Dim index = Me.DataGridView1.Rows.Add()
Me.DataGridView1.Rows(index).SetValues(columns)
End While
End Using
End Using
sw.Stop()
End Sub
Well, now my problem is that i don't want to put the full txt file in that datagridview, just from line N.
Is it possible to do that? Like creating a querytabel and selecting a fixed value?
p.e., in line 5 there's always the text "Values:" . Can i select all the lines after that to put in the datagridview? i googled everywhere but found nothing. and there's no "sample" code to give me a start . thank you all !
Dim n As Integer = 5
Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("textbox1.text").Skip(n)
'Gets every line after a certain line count
'Create a new datatable and add some columns
Dim dt As New DataTable
dt.Columns.AddRange((From columnIndex As Integer In Enumerable.Range(1, lines.First.Split(";"c).Count) Select New DataColumn("Column" & columnIndex.ToString())).ToArray())
'Add each line as a row to the datatable
For Each line As String In lines
dt.Rows.Add(line.Split(";"c))
Next
'Set the datasource of the datagridview
MyDataGridView.DataSource = dt

how to increase datatable's name number in vb.net

I know, how to increase integer, string by for statement
but I want know how to change the name by 'for ~next' statement.
For example,
Dim row_sort1 As DataTable = rows1.CopyToDataTable
Dim row_sort2 As DataTable = rows2.CopyToDataTable
Dim row_sort3 As DataTable = rows3.CopyToDataTable
Dim row_sort4 As DataTable = rows4.CopyToDataTable
Dim row_sort5 As DataTable = rows5.CopyToDataTable
Dim row_sort6 As DataTable = rows6.CopyToDataTable
Dim row_sort7 As DataTable = rows7.CopyToDataTable
I had coding like this,, bad cording So I want change by 'for ~next' statement.
I want increase the datatable name's number (1~7)
how can reflect in this coding. I want fix my coding more simple and useful.
I need your help
thank you
I assume you have a Collection of Rows which is your master where you want to copy from.
I have a similiar approach like OSKM. Better use a list collection than an array.
To access tables afterwards in the collection you can use Linq.
' Given master rowcollection
Dim masterRow As EnumerableRowCollection(Of DataRow)
' Empty table collection
Dim tableList As New List(Of DataTable)
For t As Integer = 0 To 6
' copy Master to a new table
Dim newTable As DataTable = masterRow.CopyToDataTable()
' give the new table a name
newTable.TableName = "Table" & t.ToString
' Add new table to collection
tableList.Add(newTable)
Next
' Access a certain table (i.e. Table5) using Linq
Dim table5 As DataTable = tableList.FirstOrDefault(Function(x) x.TableName = "Table5")
If i understand your question correct the following might help.
'Note your datatables will be named Datatable0 to Datatable6
Dim DTs(6) As DataTable
For i = 0 To 6
DTs(i) = New DataTable
DTs(i).TableName = "Datatable" & i
Next
There is probably better ways but this will work!

Get RepositoryItem (As Control) From Devexpress-GridviewCell

I'm using the devexpress Gridcontrol/Gridview which has 4 columns
Name: String
Description: String
Action: RepositoryItemLookUpEdit
Info: RepositoryItemHyperLinkEdit
Right now i want to write a function which updates the Action-column but only if the Value is contained in the datasource of the RepositoryItemLookUpEdit
So i started writing the code and this is how i far i got:
For i As Integer = 0 To GridViewDD.RowCount - 1
Dim j As Integer = i
Dim rItemlookup As RepositoryItemLookUpEdit = CType(GridViewDD.GetRow(i), DataRowView).Item("Actions")
If CType(rItemlookup.DataSource, List(Of String)).Contains(curraction) Then
// Do update of the datasource here (which works)
End If
Next
GridControlDD.RefreshDataSource()
My problem lies at the line:
Dim rItemlookup As RepositoryItemLookUpEdit = CType(GridViewDD.GetRow(i), DataRowView).Item("Actions")
Question:
How can i get the RepositoryItemLookUpEdit of a cell in devexpress (or its datasource)?
Note:
The datasource of my gridview (GridViewDD) is a List And my datasource of the RepositoryItemLookUpEdit in Action is always a List(Of String)
Note 2:
The contents of my datasource may vary from row to row
You can easily get your RepositoryItem from GridColumn.ColumnEdit property.
Here is example:
Dim rItemlookup As RepositoryItemLookUpEdit = GridViewDD.Columns("Action").ColumnEdit
'...
For i As Integer = 0 To GridViewDD.RowCount - 1
Dim j As Integer = i
If CType(rItemlookup.DataSource, List(Of String)).Contains(curraction) Then
'... Do update of the datasource here (which works)
End If
Next
GridControlDD.RefreshDataSource()
I've found it. thanks to 'many' hours of browsing the devexpress-forums. Using Gridviewinfo and GridDataRowInfo you can easily access the controls 'hidden' inside the grid
In my case the code looks as following
Dim gvInfo As GridViewInfo = GridViewDD.GetViewInfo()
Dim rInfo As GridDataRowInfo = gvInfo.RowsInfo.FindRow(i)
Dim rItemlookup As RepositoryItemLookUpEdit = rInfo.Cells(GridViewDragDrop.Columns.Item("Actions")).Editor
you can now use rItemlookup to change or access its properties.
I hope this may be of some use to anyone.

skip first row when reading excel CSV file

I found how to do this in several languages but not in .net (specifically vb.net). I am using OLeDbCommand to read both CSV and Excel files. In case of Excel I can skip first row and select second row onwards by specifying a range of cells. But in case of CSV, I am not sure how to do it.
Current code looks like:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [" + Path.GetFileName(FileName) + "]", cn)
Here we give the file, not the sheet. So I am bit stuck.
From my experience reading a text file like this is very restrictive. It only allows you to read the whole file, because you can't specify a table name. You might be better of reading each line and making table rows and adding them to a table. If the first row is headers you can use that to make the columns, otherwise hard code the columns.
Here's a simple little method that fills a datatable with the data from a .csv file, that you should be able to use:
Private Sub GetData(ByRef dt As DataTable, FilePath As String, Optional ByVal Header As Boolean = True)
Dim Fields() As String
Dim Start As Integer = CInt(Header) * -1
If Not File.Exists(FilePath) Then
Return
End If
dt.Clear()
Dim Lines() As String = File.ReadAllLines(FilePath)
If CBool(Start) AndAlso dt.Columns.Count = 0 Then
Lines(0) = Lines(0).Replace(Chr(34), "")
For Each h As String In Lines(0).Split(",")
dt.Columns.Add(h)
Next
End If
For I = Start To Lines.Count - 1
Fields = Lines(I).Split(",")
dt.Rows.Add(Fields)
Next
End Sub

Display all contents of a "table" created in LINQ in VB.NET

Working from a previous question I asked (which was answered very well).. Ive come across another snag... In the following code
Public Sub Main()
Dim EntireFile As String
Dim oRead As System.IO.StreamReader
oRead = File.OpenText("testschedule.txt")
EntireFile = oRead.ReadToEnd
Dim table As New List(Of List(Of String))
' Process the file
For Each line As String In EntireFile.Split(Environment.NewLine)
Dim row As New List(Of String)
For Each value In line.Split(",")
row.Add(value)
Next
table.Add(row)
Next
' Display all contents of 5th column in the "table" using LINQ
Dim v = From c In table Where c(5) = ""
For Each x As List(Of String) In v
Console.WriteLine(x(0)) ' printing the 1st column only
Next
Console.WriteLine("Value of (2, 3): " + table(1)(2))
End Sub
`
The area where it says Dim v = From c In table Where c(5) = "" the blank quotations will only accept a specific number that its looking for in that column.
For Example:
Dim v = From c In table Where c(5) = "7" Will only show me any 7's in that column. Normally there will be many different values and I want it to print everything in that column, I just cant figure out the command to have it display everything in the selected column
Once again Many MANY Thanks!
If you want to show all rows (to be precise: items in the IEnumerable), just remove the Where condition
Dim v = From c In table
Just a note: table is not a very good name for your list, it leads the thought to SQL. This is just Linq2Objects and you don't query tables you query plain objects with a syntax very similar to Linq2SQL that in turn is heavily inspired by SQL.