How do you add your queried datatable results into a new datatable - windows

Can someone please help me fill my queried results into the existing datatable or a new datatable
''dt is filled with data from a csv file.
Dim dataRows As DataRow() = dt.Select("[Calendar year TEXT] = '2020'")
dt.Clear() 'Clear the datatable
dt.Rows.Add(dataRows) 'Add the result to the existing datatable or a new databletable if possible ?

So I've figured it out.
The reason I need a new DataTable is that I need to use it again in its original form later.
I could not attach the DataRow collection to my display control which is why I needed a new DataTable with the filtered results.
Private Function FilterDataTable(ByVal strQuery As String, ByVal dtRaw As DataTable)
Dim dataRows As DataRow() = dtRaw.Select(strQuery)
Dim dtFiltered As DataTable
dtFiltered = dtRaw.Clone() 'This is very important.
For Each drow As DataRow In dataRows
dtFiltered.ImportRow(drow) 'Add each filtered row to new DataTable.
Next
Return dtFiltered
End Function

Your dt.select will return an array of Datarow pointing to the rows in the datatable dt. Your dt.clear then clears the datatable and so will empty the dataRows array leaving you nothing left.
I'm not clear exactly what you are trying to do. I'm guessing you have a table that you want to filter and then discard all rows that don't match, leaving you a datatable to work with.
If so, here's a few options:
Work with the datarow array directly rather than the datatable. Is there really any need to convert the rows back to a datatable?
Create a new datatable to add the rows to, but don't clear the original table:
Dim dt As New DataTable ' Assume this is your populated table
Dim dataRows As DataRow() = dt.Select("[Calendar year TEXT] = '2020'")
Dim dtResults As DataTable = dt.Clone()
For Each row As DataRow In dr
dtResults.ImportRow(row)
Next
bare in mind any changes you make to the rows in dtResults will also affect the rows in dt as they both contain the same data
Depending on the size of your table and whether or not you really want to discard non matching rows, you could just remove all rows that don't match then work with the result:
Dim i As Integer = dt.Rows.Count - 1
While i >= 0
If dt(i).Item("Calendar") <> "Your test here" Then dt.Rows.Remove(dt(i))
i -= 1
End While

Related

How do i set the celltype for a datagrid column?

I am adding a new column to a datagrid to store a row total of qty*Cost
When I try to add the column I get an exception saying
System.InvalidOperationException: 'Column cannot be added because its CellType property is null.'
I've tried to set the cell type but I can't get the type right
Dim dt As DataTable = Me.DsOppQuoteDetail.tblOppQuoteDetail
Dim dr As DataRow
Dim dc As New DataGridViewColumn
With dc
.HeaderText = "Item Total"
.Name = "UnitTotal"
.CellType = DataGridTextBox
End With
DGV_OppQuoteDetail.Columns.Insert(6, dc)
Setting the CellType to DataGridTextBox produces an error
If I change the the column to:
Dim dc As New DataGridTextBoxColumn
With dc
.HeaderText = "Item Total"
End With
DGV_OppQuoteDetail.Columns.Insert(6, dc)
then I can't insert it because it's the wrong type for the DataGrid.Insert command
Dim dc As New DataGridViewTextBoxColumn
dc.HeaderText = "SomeText"
dc.Name = "colWhateverName"
DGV_OppQuoteDetail.Columns.Add(dc)
Try this and let me know. Only slightly different in terms of wording. Also from your code snippet it seems fine but make sure you don't add any rows before adding a column.

DataTable: create rows dynamically according to the number of checkboxes selected in the checkbox List

I am trying to create a DataTable the number of rows of which needs to be created automatically according to the number of checkboxes checked in my checkbox list:
Private Function GetRoomTypeIds() As DataTable
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id", GetType(Integer)), New DataColumn("RoomTypeId", GetType(Integer))})
dt.Rows.Add(txtId1.Text, chkRoomTypes.SelectedValue)
Return dt
End Function
I would need to add something like:
"For Each Checkbox checked in my checkbox list generate the corresponding number of rows."
Thanks
I solved as per below code:
Private Function GetRoomTypeIds() As DataTable
Dim selectedItems = From s In chkRoomTypes.Items.Cast(Of ListItem)
Where s.Selected
Select s
Dim itemTable As DataTable
itemTable = New DataTable("SelectedItems")
Dim column1 As DataColumn = New DataColumn("RateTypeId")
column1.DataType = System.Type.GetType("System.Int32")
Dim column2 As DataColumn = New DataColumn("RoomTypeId")
column2.DataType = System.Type.GetType("System.Int32")
itemTable.Columns.Add(column1)
itemTable.Columns.Add(column2)
Dim Row As DataRow
For Each item In selectedItems
Row = itemTable.NewRow()
Row("RateTypeId") = Convert.ToInt32(txtId1.Text)
Row("RoomTypeId") = item.Value
itemTable.Rows.Add(Row)
Next
Return itemTable
End Function
Thanks

HOW to merge and get the difference between 2 datatable

we've created a function that merge 2 datatable.
here's the code
Public Shared Function CompareTwoDataTable(ByVal dt1 As DataTable, ByVal dt2 As DataTable) As DataTable
dt1.Merge(dt2)
Dim d3 As DataTable = dt2.GetChanges()
Return d3
End Function
i was wondering if the code was incorrect or what, my point is i want to determine the difference between those 2 table and get their difference.
Thanks in advance
for those who view this, this is the code i've come up to
i've created a function that compares the 2 datatable using their ids
Public Shared Function CompareTwoDataTable(ByVal dt1 As DataTable, ByVal dt2 As DataTable) As DataTable
Dim rows_to_remove As New List(Of DataRow)()
For Each row1 As DataRow In dt1.Rows
For Each row2 As DataRow In dt2.Rows
If row1("ID").ToString() = row2("ID").ToString() Then
rows_to_remove.Add(row1)
End If
Next
Next
For Each row As DataRow In rows_to_remove
dt1.Rows.Remove(row)
dt1.AcceptChanges()
Next
Dim d3 As DataTable = dt1
Return d3
End Function
Hope it helps

linq on datarow to get index of specific item

I have datatable which it first row is headers
I need specific column for this datatable according to the header
I know how to get the column if know its index.
The problem is how to get the index
Dim columnIndex as integer
Dim headerRow As DataRow = dt.Rows(0)
Dim colHeader As string ="abc"
columnIndex=???
Dim result = dt.Rows.Cast(Of DataRow)().[Select](Function(row) row(columnIndex)).Distinct().ToList()
Thanks
You may use the dt.Rows.IndexOf
Dim ValueToSearch AS string = ...
columnIndex = dt.AsEnumerable().Where(Function(x) x.Field(of String)(colHeader) = ValueToSearch).Select(Function(x) dt.Rows.IndexOf(x)).SingleOrDefault()
The above will work if the where clause returns only one or zero rows. If this is not true then you may dismiss the SingleOrDefault and then loop through the results, ie:
columnIndex = dt.AsEnumerable().Where(Function(x) x.Field(of String)(colHeader) = ValueToSearch).Select(Function(x) dt.Rows.IndexOf(x))
For Each i in columnIndex
Console.WriteLine("Value found in row with index " + i.ToString())
Next
Giannis

Make only one row show after search in FlexGrid VB6

So i have FlexGrid in my VB6 project I'm working on. It has names on each row, and I have a drop down so the user can select what name they want to see more info for, here is what I have.
Dim target_name As String
Dim r As Integer
' Get the name.
target_name = Combo1
If Len(target_name) = 0 Then Exit Sub
' Search for the name, skipping the column heading row.
target_name = LCase$(target_name)
For r = 1 To MSFlexGrid1.Rows - 1
If LCase$(MSFlexGrid1.TextMatrix(r, 0)) = _
target_name Then
' We found the target. Select this row.
MSFlexGrid1.Row = r
MSFlexGrid1.RowSel = r
MSFlexGrid1.Col = 0
MSFlexGrid1.ColSel = MSFlexGrid1.Cols - 1
' Make the row visible.
MSFlexGrid1.TopRow = r
Exit Sub
End If
Next r
That works well, but it shows everything below that name too, I would like it to single out only the name selected.
Any help would be great.
What's the data source of your grid? You can place the filter on the data grid data source, so that as the user chooses the name from your drop down only the selected persons details are returned from the datasource to the grid.
Not exactly what you were asking, but its how I would achieve the result you are wanting.
P.S. I have used FlexGrid in VB6 and I don't know of a way to do what you are asking on the grid (might be there but I never noticed it).

Resources