Selecting the non-duplicate elements in the DataTable - vb.net

I want to choose non-duplicate elements in DataTable
Where DataTable contains data as follows
TABLE A
COLUMN A
AM1
AM5
AM6
AM5
AM1
AM1
AM1
Where the final output is datatable= AM6
MY CODE
Public Function RemoveDuplicateRowsG(ByVal dTable As DataTable) As DataTable
For intI = dTable.Rows.Count - 1 To 0 Step -1
For intJ = intI - 1 To 0 Step -1
If dTable.Rows(intI)(0) = dTable.Rows(intJ)(0) Then
dTable.Rows.RemoveAt(intI)
dTable.Rows.RemoveAt(intJ)
Exit For
End If
Next
Next
dTable.AcceptChanges()
Return dTable
End Function

Maybe if you iterate the datatable first and select the row(s) which contain the value only once, put it inside a new datatable and return the newly created datatable.
Dim dtToReturn As New DataTable
For Each row As DataRow in dTable.Rows
'Select all rows from original datatable with the same value
Dim datarow() As DataRow = dTable.Select("COLUMN A = " & row.Item(0))
'Insert to the new datatable if returned datarow collection only has 1 row
If datarow.Count = 1 Then
dtToReturn.Rows.Add(datarow)
End If
Next
Return dtToReturn

Related

how to Flip dataset and display in datagridview

I try to flip dataset to display column as rows by using this code but it does not work :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button.Click
Dim ds2 As New DataSet
Dim dt2 As New DataTable
Dim com1 As String = "select col1,col2,col3 from table1"
ds2 = FlipDataSet(ds2)
Dim dp As New SqlDataAdapter(com1, conn)
dp.Fill(dt2)
DGV_lev1.DataSource = dt2.DefaultView
End Sub
and use this function to flip dataset :
Private Function FlipDataSet(old_DataSet As DataSet) As DataSet
Dim ds As New DataSet()
For Each dt As DataTable In old_DataSet.Tables
Dim table As New DataTable()
For i As Integer = 0 To dt.Rows.Count
table.Columns.Add(Convert.ToString(i))
table.Columns(0).ColumnName = "Fields"
If i = 0 Then
Continue For
Else
table.Columns(i).ColumnName = "Customer " & i
End If
Next
Dim r As DataRow
For k As Integer = 0 To dt.Columns.Count - 1
r = table.NewRow()
r(0) = dt.Columns(k).ToString()
For j As Integer = 1 To dt.Rows.Count
r(j) = dt.Rows(j - 1)(k)
Next
table.Rows.Add(r)
Next
ds.Tables.Add(table)
Next
Return ds
End Function
to make datagirdview display from this :
to this :
can anyone help me
thank you
Try this, it worked in a quick test I did:
Private Function Transpose(ByVal table As DataTable) As DataTable
Dim flippedTable As New DataTable
'creates as many columns as rows in source table
flippedTable.Columns.AddRange(
table.Select.Select(
Function(dr) New DataColumn("col" & table.Rows.IndexOf(dr), GetType(Object))
).ToArray)
'iterates columns in source table
For Each dc As DataColumn In table.Columns
'get array of values of column in each row and add as new row in target table
flippedTable.Rows.Add(table.Select.Select(Function(dr) dr(dc)).ToArray)
Next
Return flippedTable
End Function

Adding row in DataTable

I'm doing a project in vb and I'm trying to add all the rows in dataTable name dt to a new dataTable name dtNew but i dont want duplicate rows to be added in dtNew instead adding the count if its duplicate. someone help me please.
here is a sample dataTable name dt as you can see apple is duplicate
here is my code.
Dim dtNew As DataTable = New DataTable '---> I Created new DataTable
'---> Created 3 columns
dtNew.Columns.Add("TYPE", Type.GetType("System.String"))
dtNew.Columns.Add("NAME", Type.GetType("System.String"))
dtNew.Columns.Add("COUNT", Type.GetType("System.Int32"))
For Each dtRow As DataRow In dt.Rows ' ---> loop all the rows in dt DataTable
Dim newRow As DataRow = dtNew.NewRow
newRow("TYPE") = dtRow("TYPE")
newRow("NAME") = dtRow("NAME")
newRow("COUNT") = dtRow("COUNT")
'check if dtNew DataTable has no row
If Not dtNew Is Nothing AndAlso dtNew.Rows.Count = 0 Then
'add new row
dtNew.Rows.Add(newRow)
Else
' I want to check first all the rows in dtNew DataTable
' if its existed, and if it's not then add new row
For Each dtNewRow As DataRow In dtNew.Rows
If ((dtNewRow("TYPE") = "VEGETABLE" OrElse _
dtNewRow("TYPE") = "FRUIT") And _
dtNewRow("NAME") <> newRow("NAME")) Then
'insert row
dtNew.Rows.InsertAt(newRow, dtNew.Rows.Count)
'error: Collection was modified; enumeration operation might not be executed.
End If
Next
End If
Next
For Each row As DataRow In dt.Rows
Dim type = CStr(row("Type"))
Dim name = CStr(row("Name"))
Dim existingRows = dtNew.Select(String.Format("Type = '{0}' AND Name = '{1}'",
type,
name))
If existingRows.Length = 0 Then
'No match so create a new row.
Dim newRow = dtNew.NewRow()
newRow("Type") = type
newRow("Name") = name
newRow("Count") = row("Count")
dtNew.Rows.Add(newRow)
Else
'Match found so update existing row.
Dim newRow = existingRows(0)
newRow("Count") = CInt(newRow("Count")) + CInt(row("Count"))
End If
Next
As the tables have the same schema, you could even simplify the If block to this:
dtNew.ImportRow(row)
That would only be a problem if row had a RowState other than Unchanged and you didn't want to import that too.

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

Returning datas from datatable in vb.net

DTName is a DataTable which returns rows in the code below...when I iterate it through the loop from 0 to its count. Only the data in first row is displayed.
How to get all the rows of the datatable displayed in the result ?
DTName = GetClientNames()
If Not DTName Is Nothing Then
For i = 0 to DTName.Rows.count
strName = DTName.Rows(i).Item("Client Name").Tostring()
Next i
End if
For i = 0 to DTName.Rows.count would eventually throw a IndexOutOfRangeException error when the value of i equals to DTName.Rows.count, the limit should be DTName.Rows.count - 1 .
To get all the values from all datarows, store them in a List :
Dim strName As New List(Of String)
For i = 0 to DTName.Rows.count - 1
strName.Add(DTName.Rows(i)("Client Name").Tostring())
Next i
Alternatively you could use Foreach like this :
For Each DR As DataRow In DTName.Rows
strName.Add(DR("Client Name").Tostring())
Next
I also suggest you remove the redundant check if DTName.Rows.Count > 0
EDIT : You could Declare strName as string and append row values to it :
For i = 0 to DTName.Rows.count - 1
strName &= (DTName.Rows(i)("Client Name").Tostring() & ",")
Next i
Response.Write(strName)
Double to check the actual ColumnName of the Column.
For Each item As DataRow In dataTable.Rows
strName = item("Client Name").ToString()
Console.WriteLine(strName)
Next

How to find the original position in a DataTable from a filtered DataView

I want to find a particular ID in a DataTable, I am using a DataView to filter the results, but how do I know which row in the original table the filter view corresponds to? Any ideas? In know I could use LINQ but I don't think this would help either?
Dim dt As New DataTable
Dim dv As New DataView(dt)
dv.RowFilter = "ID = 123"
If dv.Count = 1 Then
'which datarow in the original datatable is this?
End If
EDIT
I want to avoid having to loop through the DataTable to find this:
For r As Integer = 0 To dt.Rows.Count - 1
If CInt(dt.Rows(r).Item("ID")) = 123 Then
Debug.WriteLine("Found it at row " + r.ToString)
Exit For
End If
Next
Nothing could be easier:
Dim dr As DataRow = dv(0).Row
http://msdn.microsoft.com/en-us/library/system.data.datarowview.row%28v=VS.100%29.aspx
Edit: According to your comment, you could use following to get the index of the row
Dim rowIndex As Int32 = -1
For i As Int32 = 0 To dr.Table.Rows.Count - 1
If dr.Table.Rows(i)("ID").Equals(dr("ID")) Then
rowIndex = i
Exit For
End If
Next
Or in this shorter way:
Dim rowIndex As Int32 = dr.Table.Rows.IndexOf(dr)
http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.indexof.aspx