convert a datatable - vb.net

I have a datatable like this
X,Y,Z
0,0,A
0,2,B
0,0,C
1,0,A
1,0,C
2,2,A
2,2,B
2,0,C
3,2,B
3,1,C
4,3,A
4,0,B
4,1,C
5,3,A
5,2,B
5,0,C
and I want to convert it to something like this:
X,A,B,C
0,0,2,0
1,0, ,0
2,2,2,0
3, ,2,1
4,3,0,1
5,3,2,0
I tried with dataset and linq but not I wasn't lucky.
My code for linq:
Dim q = (From c In dt _
Select c("Z") Distinct) 'I found out which categories I have in Z column (my example :A,B,C)
Dim ldt(q.Count) As DataTable
For i = 0 To q.Count - 1
Dim sfil As String = q(i).ToString
Dim r = (From c In dt _
Select c Where c("Z") = sfil)
ldt(i) = r.CopyToDataTable
Next
So now I have 3 tables (ldt(0) with values for A, ldt(1) with values for B, ldt(2) with values for C)
and I was thinking to do something like leftJoin but anything that I tried is fail.
Any solution or even a better idea?
Thanks
So a new example it would be:
I have this table:
id,Price,Item
0,0,Laptop
0,2,Tablet
0,0,Cellphone
1,0,Laptop
1,0,Tablet
2,2,Laptop
2,2,Cellphone
2,0,Tablet
3,2,Cellphone
3,1,Tablet
4,3,Laptop
4,0,Cellphone
4,1,Tablet
5,3,Laptop
5,2,Cellphone
5,0,Tablet
and I would like to convert it to this:
X,Laptop,Tablet,Cellphone
0,0,2,0
1,0, ,0
2,2,2,0
3, ,2,1
4,3,0,1
5,3,2,0
The values for each of the columns Laptop, Tablet, Cellphone are the Y values from the first table.
I hope it make more sense now.

I believe you can create a DataTable with column names corresponding to the item names. Then you group the previous DataTable by id and use each grouping to populate a row. Forgive me if I get anything wrong. I don't work with VB or DataTables that much.
Dim itemNames = (From c In dt _
Select c("Item") Distinct)
Dim newDt as DataTable = new DataTable()
Dim idColumn As DataColumn = new DataColumn()
idColumn.DataType = System.Type.GetType("System.Int32")
idColumn.ColumnName = "id"
idColumn.ReadOnly = True
idColumn.Unique = True
newDt.Columns.Add(idColumn)
For Each itemName As String In itemNames
Dim column As DataColumn = new DataColumn()
column.DataType = GetType(Nullable(Of Integer))
column.ColumnName = itemName
column.ReadOnly = True
column.Unique = False
newDt.Columns.Add(column)
Next
Dim groupingById = From row in dt
Group By Id = row("id")
Into RowsForId = Group
For Each grouping In groupingById
Dim row as DataRow = newDt.NewRow()
row("id") = grouping.Id
For Each rowForId in grouping.RowsForId
row(rowForId("Item")) = rowForId("Price")
Next
newDt.Rows.Add(row)
Next

Related

Problem on filling data table from my MS access Database [duplicate]

I'm using vb.net / winforms.
How can I convert 10 lines with three columns into a DataSet/DataTable?
Lines are something like this:
Item-1, $100, 44
Item-2, $42, 3
etc
Dim Table1 As DataTable
Table1 = New DataTable("TableName")
Dim column1 As DataColumn = New DataColumn("Column1")
column1.DataType = System.Type.GetType("System.String")
Dim column2 As DataColumn = New DataColumn("Column2")
column2.DataType = System.Type.GetType("System.Int32")
Dim column3 As DataColumn = New DataColumn("Column2")
column3.DataType = System.Type.GetType("System.Int32")
Table1.Columns.Add(column1)
Table1.Columns.Add(column2)
Table1.Columns.Add(column3)
Dim Row1 As DataRow
Row1 = Table1.NewRow()
Row1.Items("Column1") = "Item1"
Row1.Items("Column2") = 44
Row1.Items("Column3") = 99
Table1.Rows.Add(Row1)
' Repeat for other rows
I know this post is old, but I believe this can be solved in fewer lines of code.
' Declare DataTable
Dim Table1 As new DataTable()
' Define columns
Table1.Columns.Add("Column1", GetType(System.String))
Table1.Columns.Add("Column2", GetType(System.Int32))
Table1.Columns.Add("Column3", GetType(System.Int32))
' Add a row of data
Table1.Rows.Add("Item1", 44, 99)
Table1.Rows.Add("Item2", 42, 3)
Source DotNetPerls.com : VB.NET DataTable

Insert headers of a datatable VB.NET

I need your help for my code vb
In fact, i created an new datatable and i want to copy the headers from another datatable
here is my code
Dim name(de.Tables(0).Columns.Count) As String
Dim p As Integer = 0
For Each column As DataColumn In de.Tables(0).Columns
name(p) = column.ColumnName
p += 1
Next
Dim m As Integer = 0
For m = 0 To de.Tables(0).Columns.Count - 1
dt.Columns(m).ColumnName = name(p)
Next
If you only want to "copy" the schema of a DataTable(so the columns and constraints) without it's content(DataRows) you can use DataTable.Clone:
Dim clonedTable As DataTable = originalTable.Clone()
If you also want to copy the DataRows you have to use DataTable.Copy.
Try this one
Dim dt As New DataTable()
Dim name(de.Tables(0).Columns.Count) As String
For Each column As DataColumn In de.Tables(0).Columns
dt.Columns.Add(New DataColumn(column.ColumnName))
Next

Distinct Rows From Vb.Net DataTable

I have a scenario, in which I have to apply distinct filter onto DataTable and find the rows only which are distinct,
I am using dt.DefaultView.ToTable(True, Columns) this statement but no effect.
Here is my chunk of code..
Try
Dim dTable As New DataTable()
dTable.Columns.Add("AutoID")
dTable.Columns.Add("AnotherID")
dTable.Columns.Add("CitY")
Dim row As DataRow = Nothing
For i As Integer = 0 To 4
row = dTable.NewRow()
row("AutoID") = i + 1
row("AnotherID") = i + 10
row("City") = "Vetican"
dTable.Rows.Add(row)
Next
dTable.Rows.Add(6, "11", "Oslo")
dTable.Rows.Add(7, "12", "Toronto")
Dim TobeDistinct As String() = {"AnotherID"}
Dim dtDistinct As DataTable = GetDistinctRecords(dTable, TobeDistinct)
Catch ex As Exception
End Try
and the method ..
Public Shared Function GetDistinctRecords(ByVal dt As DataTable, ByVal Columns As String()) As DataTable
Dim dtURecords As New DataTable()
dtURecords = dt.DefaultView.ToTable(True, Columns)
Return dtURecords
End Function
Here is the screen shot , which I want..
Which rows do you want to keep and which rows should be removed? If you just want to keep one row per AnotherID it seems to be arbitrary to keep Vetican instead of Oslo. Maybe you want to concat both as in Vetican, Oslo.
I would use Linq instead:
Dim resultTable = dTable.Clone() ' empty table same columns
Dim idGroups = dTable.AsEnumerable().GroupBy(Function(r) r.Field(Of String)("AnotherID"))
For Each grp In idGroups
Dim r As DataRow = resultTable.Rows.Add()
r.SetField("AutoID", grp.First().Field(Of String)("AutoID"))
r.SetField("AnotherID", grp.Key)
Dim cities = From row In grp Select row.Field(Of String)("City")
r.SetField("City", String.Join(", ", cities))
Next

LINQ how to handle null values

sorry for bad english;
my code;
Dim otherDT As DataTable = retDS1.Tables(0)
Dim dt As New DataTable
dt.Columns.Add("COGRAFI_BOLGE_ADI")
dt.Columns.Add("Count")
Dim query = (From dr In (From d In otherDT.AsEnumerable Select New With {.COGRAFI_BOLGE_ADI = d("COGRAFI_BOLGE_ADI")}) Select dr.COGRAFI_BOLGE_ADI Distinct)
For Each colName As String In query
Dim cName = colName
Dim cCount = (From row In otherDT.Rows Select row Where row("COGRAFI_BOLGE_ADI").ToString = cName).Count
dt.Rows.Add(colName, cCount)
Next
GridView1.DataSource = dt
GridView1.DataBind()
Some rows contain a null value;
How do I handle null lines?
Output:
Marmara 40
Ege 10
Akdeniz 2
...
Not really clear what you are asking, but are you looking for something like this:
Dim otherDT As DataTable = retDS1.Tables(0)
Dim dt As New DataTable
dt.Columns.Add("COGRAFI_BOLGE_ADI")
dt.Columns.Add("Count")
Dim query = (From dr In (From d In otherDT.AsEnumerable
Select New With {.COGRAFI_BOLGE_ADI = d("COGRAFI_BOLGE_ADI")})
Select dr.COGRAFI_BOLGE_ADI Distinct)
For Each colName As String In query
Dim cName As String = colName
Dim cCount As Integer = (From row In otherDT.Rows
Select row
Where row IsNot Nothing AndAlso
row("COGRAFI_BOLGE_ADI") IsNot Nothing AndAlso
row("COGRAFI_BOLGE_ADI").ToString = cName).Count
If cCount > 0 Then
dt.Rows.Add(colName, cCount)
End If
Next
GridView1.DataSource = dt
GridView1.DataBind()

vb.net creating data table?

I'm using vb.net / winforms.
How can I convert 10 lines with three columns into a DataSet/DataTable?
Lines are something like this:
Item-1, $100, 44
Item-2, $42, 3
etc
Dim Table1 As DataTable
Table1 = New DataTable("TableName")
Dim column1 As DataColumn = New DataColumn("Column1")
column1.DataType = System.Type.GetType("System.String")
Dim column2 As DataColumn = New DataColumn("Column2")
column2.DataType = System.Type.GetType("System.Int32")
Dim column3 As DataColumn = New DataColumn("Column2")
column3.DataType = System.Type.GetType("System.Int32")
Table1.Columns.Add(column1)
Table1.Columns.Add(column2)
Table1.Columns.Add(column3)
Dim Row1 As DataRow
Row1 = Table1.NewRow()
Row1.Items("Column1") = "Item1"
Row1.Items("Column2") = 44
Row1.Items("Column3") = 99
Table1.Rows.Add(Row1)
' Repeat for other rows
I know this post is old, but I believe this can be solved in fewer lines of code.
' Declare DataTable
Dim Table1 As new DataTable()
' Define columns
Table1.Columns.Add("Column1", GetType(System.String))
Table1.Columns.Add("Column2", GetType(System.Int32))
Table1.Columns.Add("Column3", GetType(System.Int32))
' Add a row of data
Table1.Rows.Add("Item1", 44, 99)
Table1.Rows.Add("Item2", 42, 3)
Source DotNetPerls.com : VB.NET DataTable