How to make datatable blank in Vb.net? - vb.net

I have a data table which has few rows and columns. I need to delete the entire data along with column names also.
Eg :
Item | 2017 | 2018 | 2019 | 2020
----------------------------------
A 400 200 300 500
B 400 300 500 100
C 100 200 500 300
D 100 300 400 100
I need to delete all the data of the above data table along with the column names so that I can re-use the same data table for storing other values.
I tried datatable.clear() method it is clearing only the data, the column names remain intact and also I can't use dispose().
How can I do it.?

Short version here:
Dim dt As New DataTable
'...
'you do some operations
'...
dt = New DataTable 'renew the same instance
Alternatively, you can use:
dt = Nothing 'basically setting it to NULL

This should work:
datatable.Clear() 'clear records
datatable.Constraints.Clear() 'clear constraints (required before Columns.Clear(), if there is any constraint)
datatable.Columns.Clear() 'clear columns
Or this:
datatable.Reset()
But if you want to just create a new "blank" instance, or in other words, reinitialize it:
datatable = New Datatable

The solution that work for me is the following
datatable.Reset()

Related

datagridview column copy to another datagridview column without duplicate

How can I copy / clone a column from 1 DataGrid View to another DataGrid View without duplicate? Is there a way to do this? So far, I hadn't found any solution in Google regarding about this issue...any help would be greatly appreciated...
I am currently using vb.net to code in VisualStudio2019
Example
DGV1 | DGV2
Fruit Qtt | Fruit Qtt
Apple 2 | Apple 4
Pear 3 | Pear 6
Wintermelon 1 | Wintermelon 1
Apple 2 |
Pear 3 |
DGV1 column 1 move to DGV2 column1 without duplicate / unique
How can I do this?
' Iterate through the rows in the source GridView (GridView1)
For Each row As GridViewRow In GridView1.Rows
Dim data As String = row.Cells(0).Text ' Get the data from the first cell in the row
Dim exists As Boolean = False ' Flag to track whether the data already exists in the destination GridView
' Search for the data in the destination GridView's data source
For Each item As Object In GridView2.DataSource
If item.ToString() = data Then
exists = True
Exit For
End If
Next
' If the data does not exist in the destination GridView, add it
If Not exists Then
Dim dataSource As List(Of String) = TryCast(GridView2.DataSource, List(Of String))
dataSource.Add(data)
End If
Next
' Bind the updated data source to the destination GridView
GridView2.DataSource = dataSource
GridView2.DataBind()

linq filter columns rather than data

Firstly, I'm not sure if what I'm asking is possible or not so apologies if I'm asking a stupid question.
So I am able to filter a DataTable using linq to get the data I need, I'm wondering if it's possible to filter the columns using a simlar statement.
For example if I have the below datatable dtMyData
ID
Name
1
2
3
4
1
Conor
100
87
3
0
2
Frank
35
70
0
0
3
Jeff
35
13
0
57
I can filter it to the below using the following statement
dtMyData = dtMyData.AsEnumerable().Where(Function (f) f("Name").ToString().Equals("Frank")).CopyToDataTable
ID
Name
1
2
3
4
2
Frank
35
70
0
0
What I'm wanting to do (If it's possible) is filter the columns in a similar way so that I can select all of the columsn > 2 plus the first 2 columns. Giving me the following columns
ID
Name
3
4
1
Conor
3
0
2
Frank
0
0
3
Jeff
0
57
Take a look at this method:
Private Function CopyTable(source As DataTable, columnsToKeep As IEnumerable(Of String)) As DataTable
Dim copiedTable As DataTable = source.Clone()
Dim columnsToRemove() As DataColumn = copiedTable.Columns.Cast(Of DataColumn).Where(Function(column) Not columnsToKeep.Contains(column.ColumnName)).ToArray()
For i As Integer = 0 To columnsToRemove.Length - 1
copiedTable.Columns.Remove(columnsToRemove(i))
Next
For Each row As DataRow In source.Rows
Dim values As New List(Of Object)
For Each column As DataColumn In copiedTable.Columns
values.Add(row.Item(column.ColumnName))
Next
copiedTable.Rows.Add(values.ToArray())
Next
Return copiedTable
End Function
What this does is
Clone the DataTable
Loop over the copied DataTable and remove the columns that are not in the columnsToKeep
Loop over the original DataTable and add the rows to the copied DataTable without the cells that are not in the columnsToKeep
Fiddle: https://dotnetfiddle.net/2l6wk9
Edit
It would actually be easier to use DataTable.Copy over DataTable.Clone, my apologies:
Private Function CopyTable(source As DataTable, columnsToKeep As IEnumerable(Of String)) As DataTable
Dim copiedTable As DataTable = source.Copy()
Dim columnsToRemove() As DataColumn = copiedTable.Columns.Cast(Of DataColumn).Where(Function(column) Not columnsToKeep.Contains(column.ColumnName)).ToArray()
For i As Integer = 0 To columnsToRemove.Length - 1
copiedTable.Columns.Remove(columnsToRemove(i))
Next
Return copiedTable
End Function
What this updated code does is:
Copy the DataTable with its data
Loop over the copied DataTable and remove the columns that are not in the columnsToKeep
Fiddle: https://dotnetfiddle.net/NEIm2t

How to bind and display multi line chart to form?

I am using VB2017 and have data returned from Stored Procedure and I have filled a DataTable with data. The data in Datatable sample is
Date Bill Fred Jack
Date1 25 115 33
Date2 88 15 24
Date3 41 31 162
I have set UltraChart datasource to the datatable and then Bind data
Dim datTab As New DataTable
Using da As New SqlDataAdapter(cmd)
da.Fill(datTab)
End Using
ugResolved.Data.SwapRowsAndColumns = True
ugResolved.Data.DataSource = datTab
ugResolved.Data.DataBind()
When I run The chart isn't displayed. Get Box with the following text in it. Data Not Available. Please call Ultrachart.Data.DataBind() after setting valid Data.DataSource.
I have set the dataSource. What I am missing. Just starting using these controls. I have gotten the UltraGrid and UltraChart StackColumnChart working. Can anyone advise what I am missing.
I posted to Infragistics forums but still waiting couple of days later.
I would like a line for each name with line going from day to using the numbers as their path for each date

read datable and store value in array

Name | CategorieID | FullCategorie_ID
---- ------------- ----------------
A 1 12
B 1 13
C 5 14
D 3 15
E 6 16
I want to read data from datatable and store a complete row in array and then return it
I cannot get the point to save a DataTable (that has rows and columns) inside an Array (that has only rows) but this is your question, not mine!
So, the code you are looking for is something like this:
Dim dt As New System.Data.DataTable
Dim arrayString(dt.Rows.Count - 1) As String
For dr As Integer = 0 To dt.Rows.Count
For dc As Integer = 0 To dt.Columns.Count
arrayString(dr) = arrayString(dr) & "$" & dt.Rows(dr).Item(dc)
'I added a special char ($) to easily split up data later
Next
Next
If you want to split up in columns your data, just use String.Split like shown below:
Dim first_array_row () as String = arrayString.Split("$")
But I higly suggest you to review your code/idea, It's better for you to find another way, because this workaround is horrible.
Instead why not use a List Class and a Dictionary Class
something like:
Dim values As New List(Of Dictionary(Of String, String))()

Unable to select values in second column listview

I've a listview that dynamically populates values between two columns in this fashion:
column 1 | column 2
value1 | value 2
value3 | value 4
however I'm unable to select any values in column 2 - is there a property of the listview that's preventing me from doing this or is it the way I populate these columns? Here's my code to populate the column:
For k = 0 To UBound(tempValues)
Dim itm As New ListViewItem(tempValues(k))
If k Mod 2 = 0 Then
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
itm.SubItems.Add(tempValues(k + 1))
listview1.Items.Add(itm)
End If
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Next
Any ideas?
The closest I can see for this is to set listView1.FullRowSelect = true (I assume you have listView1.View = View.Details?)
This however will only give you full row selecting - Remember the 2nd column represents the 1st Sub Item of the listview's items.
If you want multiple columns of data, you might be better off setting listView1.View = View.Details = View.List, which will cause it to wrap a single list of items onto multiple columns when it runs out of vertical space.
Edit:
If you use listView1.View = View.List, your population would need to change to the following:
For k = 0 To UBound(tempValues)
listview1.Items.Add(new ListViewItem(tempValues(k))
Next
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
But it would mean you end up with the list like so:
Value 1
Value 2
Value 3
Value 4
And if ListView was made too short to display, all these, it would wrap them:
Value 1 Value 4
Value 2
Value 3