Stuttering DataGridView in VB - vb.net

I have a DataGridView that is bound to a datatable with hundred of rows, the database is a simple flatfile database written to a txt file. Whenever I scroll to the bottom the DGV starts stuttering. I am thinking of solutions but cannot find a way to code them
Here are my proposed solution:
Use Paging to lessen the numbers of row being rendered. Here's is a similar solution but they are using sql
Using doublebuffer which I've never touched before. I've tried doing DGV.doublebuffer = true but it said DGV is protected
Any help or clarification on my problem are greatly appreciated
Edit: Here is a GIF of how my DGV is stutteting
The Datatable is named Tbl_Sample
Here is how I insert rows of data into data table. It gets data using System.IO on the Flatfile database(.txt file), split each line then send it to InputTbl as a row
Public Sub Update_Table(InputTbl As DataTable, InputFile As String)
InputTbl.Rows.Clear()
Dim lines() As String
Dim vals() As String
lines = File.ReadAllLines(InputFile)
For i = 0 To lines.Length - 1
vals = lines(i).ToString().Split("|")
Dim row(vals.Length - 1) As String
For j = 0 To vals.Length - 1
row(j) = vals(j).Trim()
Next j
InputTbl.Rows.Add(row)
Next i
End Sub
I set the table as Data Source for the DGV by DGV.DataSource = Tbl_Sample
The Datatable is created as Follow
Public Sub Sample_Table()
'Method For creating database file
Create_DBFile("Sample.db")
Try
Tbl_Sample.Columns.Add("ID", Type.GetType("System.Int32"))
Tbl_Sample.Columns.Add("Name", Type.GetType("System.String"))
Tbl_Sample.Columns.Add("Username", Type.GetType("System.String"))
Tbl_Sample.Columns.Add("Account_Type", Type.GetType("System.String"))
Tbl_Sample.Columns.Add("Date", Type.GetType("System.String"))
Tbl_Sample.Columns.Add("Time", Type.GetType("System.String"))
Catch ex As Exception
End Try
Update_Table(Tbl_Sample, "Sample.db") 'populate table
End Sub
The way I create columns is not the best. I just copied it from my old program

Solution link here
Imports System.Reflection
Public Sub EnableDoubleBuffered(ByVal dgv As DataGridView)
Dim dgvType As Type = dgv.[GetType]()
Dim pi As PropertyInfo = dgvType.GetProperty("DoubleBuffered", _
BindingFlags.Instance Or BindingFlags.NonPublic)
pi.SetValue(dgv, True, Nothing)
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
EnableDoubleBuffered(MyDataGridView, True)
End Sub

Related

How to create a datatable array/series

I want to create datatable array dynamically. according to i number, a table from datagridview will be saved to a datatable (like table1, table2 table3 etc). At another form, if user writes the i number to textbox, datagridview will be filled using the right table (table1, table2...)
But if I declare table(i) as new datatable, I get "Arrays cannot be declared with 'New'" error
If I declare table(i) as datatable, I get "system.nullreferenceexception object reference not set to an instance of an object" error while adding columns
Sub add newtable()
Dim i As Integer = TextBox1.Text
Dim table(i) As New DataTable '!
With table(i).Columns
.Add("sample1", Type.GetType("System.String"))
.Add("sample2", Type.GetType("System.String"))
End With
table(i).Rows.Add("a", "b")
table(i).Rows.Add("c", "d")
End sub
then
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DataGridView1.DataSource = table(i)
End Sub
First, now and forever, turn on Option Strict. Project Properties -> Compile tab. Also for future projects Tools -> Options -> Projects and Solutions -> VB Defaults
A DataSet is made to hold DataTables. A List(Of DataTable) might be easier to use because you wouldn't have to keep track of the index. If you really need an array then declare it at the Class level. I like to use the plural for names of collections.
Private tables As DataTable()
Sub addnewtable()
Dim i As Integer = CInt(TextBox1.Text)
tables(i) = New DataTable
With tables(i).Columns
.Add("sample1", Type.GetType("System.String"))
.Add("sample2", Type.GetType("System.String"))
End With
tables(i).Rows.Add("a", "b")
tables(i).Rows.Add("c", "d")
End Sub

Form Loads slowly in vb.net

I am currently working in ERP project on vb.net. I want to load product data in a textbox on form load. I am using autocomplete method but having a data of around 26000 the form loads slowly for 4 mins. Is there any way to avoid this or is there any way to call this function in background when the application starts?
This is my autocomplete textbox code. It works fine but it hangs alot as of the data is so large.
Private Sub pn()
Try
con = Class1.dbconn
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New SqlDataAdapter("select [Part Name] from
Part_Master_Download$", con)
da.Fill(dt)
Dim r As DataRow
TextBox9.AutoCompleteCustomSource.Clear()
For Each r In dt.Rows
TextBox9.AutoCompleteCustomSource.Add(r.Item(0).ToString)
Next
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
the properties of the textbox should be set to true for autocompletetextbox
Don't populate the AutoCompleteCustomSource in a loop. Populate an array first and then load the list in one go with a single call to AddRange:
Dim items = dt.Rows.Cast(Of DataRow)().
Select(Function(row) CStr(row(0)).
ToArray()
TextBox9.AutoCompleteCustomSource.AddRange(items)
You should find that that speeds things up considerably. If there's still a problem with performance, we can look a bit further.
EDIT: To prove my point, I just tested the following code:
Public Class Form1
Private timer As Stopwatch
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
timer = Stopwatch.StartNew()
Dim rng As New Random
Dim a = Convert.ToInt32("a"c)
Dim z = Convert.ToInt32("z"c)
Dim items = Enumerable.Range(1, 26000).Select(Function(n) Convert.ToChar(rng.Next(a, z + 1)).ToString())
For Each item In items
TextBox1.AutoCompleteCustomSource.Add(item)
Next
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
MessageBox.Show(timer.Elapsed.ToString())
End Sub
End Class
and the message displayed "00:03:08.3167858", i.e. just over three minutes to load the list. I then changed the Load event handler to this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
timer = Stopwatch.StartNew()
Dim rng As New Random
Dim a = Convert.ToInt32("a"c)
Dim z = Convert.ToInt32("z"c)
Dim items = Enumerable.Range(1, 26000).Select(Function(n) Convert.ToChar(rng.Next(a, z + 1)).ToString())
TextBox1.AutoCompleteCustomSource.AddRange(items.ToArray())
End Sub
so a single call to AddRange instead of calling Add in a loop, and the message was "00:00:00.0557427", i.e. just under 56 milliseconds. Is that better?
You can use paging to control the amount of returned data. Check this link for detailed examples.
Another way is to use (Task Async and await) so you won't lock your UI.
Here is a few last suggestions not related directly to the question but might help clean the code a bit:
you can skip the dataset and use the datatable directly, you don't need it for just one table.
Bind your results in the datatable to a datagridview or a combobox instead of iterating through your results and filling a textbox.
cheers

Making a Datatable's content appear in a DataGridView

I am continuing my rewrite of an old VB6 into .Net as part of my .Net learning. I have managed to read a CSV file into a Datatable, and I can view it has populated properly using the 'datatable visualiser' when I look at 'dt', but it doesn't seem to populate the DataGridView control I am trying to tie it to. I have spent a couple of hours browsing other threads and trying things, but I cannot see what I am doing wrong. The DataGridView is on another form in the App, and I am reading the CSV file (a config file of sorts) from the main MDI form. When I show the child form containing the DataGridView it is always empty (it actually seems to have added the two datatable rows, but their content is all blank).
Public dt As New DataTable
Public ThisFilename As String = "c:\SitesDB.cfg"
Private Sub GetSitesDB()
Dim sr As New IO.StreamReader(ThisFilename)
Dim newline() As String = sr.ReadLine.Split(","c)
dt.Columns.AddRange({New DataColumn(newline(0)), New DataColumn(newline(1)), New DataColumn(newline(2)), New DataColumn(newline(3)),
New DataColumn(newline(4)), New DataColumn(newline(5)), New DataColumn(newline(6)), New DataColumn(newline(7)), New DataColumn(newline(8)),
New DataColumn(newline(9)), New DataColumn(newline(10)), New DataColumn(newline(11))})
While (Not sr.EndOfStream)
newline = sr.ReadLine.Split(","c)
Dim newrow As DataRow = dt.NewRow
newrow.ItemArray = {newline(0), newline(1), newline(2), newline(3), newline(4), newline(5), newline(6), newline(7), newline(8), newline(9), newline(10), newline(11)}
dt.Rows.Add(newrow)
End While
Form1.DataGridView1.DataSource = dt ' data can be seen here using datatable visualiser hovering over 'dt'
End Sub
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
Form1.Show()
End Sub
I have tried refreshing the DataGridView, to no avail. I am wondering if its something to do with Form1.Show() being actioned after the datatable is tied to the DataGridView?
But the following didn't help
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' chkBit.Checked = False
' Dim cfgtext As String : cfgtext = ""
DataGridView1.DataSource = NAMS.dt
End Sub

Reading txt file line by line in DataGrid View

I'm completely new to VB.net and have been given a homework assignment. I need to be able to read certain lines and display them in a DataGridView. I have been able to link my .txt file to the DGV however it reads the whole file as opposed to the specific line. I have 4 buttons: btn1, btn2, btn3, btn4. I want each button to show the respective lines in the text file. After researching on-line for the past week I'm still stuck. If anyone could help me I would really appreciate it.
Text File ("database.txt")
(Line1) c1 c2 c3
(Line2) one 1-1 1-2
(Line3) two 2-2 2-3
(Line4) three 3-2 3-3
(Line5) four 4-2 4-3
Public Class Form1
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
Dim lines = (From line In IO.File.ReadAllLines("database.txt") _
Select line.Split(CChar(vbTab))).ToArray
For x As Integer = 0 To lines(0).GetUpperBound(0)
DataGridView1.Columns.Add(lines(0)(x), lines(0)(x))
Next
For x As Integer = 1 To lines.GetUpperBound(0)
DataGridView1.Rows.Add(lines(x))
Next
End Sub
End Class
You did not describe the result you get, so I cannot tell what's wrong right now, as I am not on a computer and usually use c++/cli (visual c++). If I have to make a guess, I would say that you fill each file row into one DGV row, but you want just 1 row, not all. Correct?
Anyway, here are some suggestions:
Do you have to read the file on each button click? If not, I would read it once, store the content and fill into the DGV when desired.
Debug your code! Set a breakpoint at the start of the function, step over the code lines, check the variable content.
Split up the code line dim lines into 3 or more separate lines to make it more readable. Also helps at debugging.
I made following assumptions based on what you had in your question:
First line of the file was the column names
The (Line#) was just there for reference and isn't in the actual file
The number in the button name (btn#) was the row index that should be displayed in the DGV
I added a new button to the form that loads the data from the text file, parses it to a datatable and sets the DataGridView1.DataSource to that DataTable. The second method then creates a new datatable and imports the specified row from the main datatable and shows it in the DGV. Of course many will say you should use a DataView with a DataView.RowFilter for this rather than creating a new datatable, however this way works just the same.
Private txtDataTable As DataTable
Private Sub loadFileBtn_Click(sender As Object, e As EventArgs) Handles loadFileBtn.Click
txtDataTable = New DataTable("txtContents")
Dim txtContents As String()
Try
txtContents = IO.File.ReadAllLines("C:\StackOverflow\database.txt")
Catch ex As Exception
MsgBox(ex.Message)
Return
End Try
Dim txtLines As New List(Of String())
txtContents.ToList().ForEach(Sub(x) txtLines.Add(x.Split(CChar(vbTab))))
If txtLines.Count > 0 Then
txtLines.Item(0).ToList.ForEach(Sub(x) txtDataTable.Columns.Add(New DataColumn(x.ToString)))
txtLines.RemoveAt(0)
End If
If txtLines.Count > 0 Then
txtLines.ToList.ForEach(Sub(x) txtDataTable.Rows.Add(x.ToArray))
End If
DataGridView1.DataSource = txtDataTable
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click
If txtDataTable Is Nothing Then Return
Dim rowIndex As Integer
If Integer.TryParse(DirectCast(sender, Button).Name.Replace("btn", String.Empty), rowIndex) Then
rowIndex -= 1
Else
Return
End If
Dim TempTable As DataTable = txtDataTable.Clone
If rowIndex < txtDataTable.Rows.Count Then
TempTable.ImportRow(txtDataTable.Rows(rowIndex))
End If
DataGridView1.DataSource = TempTable
End Sub

Cannot get the text of multiple selected items in a VB.NET ListBox because it's of type "datarowview"

I've got listbox1 in VB 2010, which is bound to a data source and displays values from a dataset. I bound it using the Designer - i.e. not via the code. I just selected the datasource in the properties of listbox1.
Now I want to retrieve the selected values. When I leave the listbox as single select, then ListBox1.SelectedValue.ToString does the job - it gives me the text of the selected item.
But I need it to allow multiple selections.
This is my code:
Dim items As ListBox.SelectedObjectCollection
items = ListBox1.SelectedItems
For Each i As String In items
MsgBox(i)
Next
And this is the error I get:
Conversion from type 'DataRowView' to type 'String' is not valid.
I've tried a few different ways to get the values of the selected items but there doesn't seem to be any straightforward way to do it. Is it impossible? Is it necessary to declare a new dataset and fill the listbox programmatically or something?
For Each drv As DataRowView In ListBox1.SelectedItems
MessageBox.Show(drv.Item(0).ToString)
Next
Off the top of my head I think you can do:
Dim items As ListBox.SelectedObjectCollection
items = ListBox1.SelectedItems
For Each i As ListViewItem In items
MsgBox(i.Value.ToString())
Next
Check out my question and answer. It allows you to break out the individual entries from a Database into list boxes.
Add 2 List Boxes and a text box to a form then copy this code, You will have to substitute your own Server and database etc entries.
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' get the data
Dim SQLConnectionString As String = "Data Source=HL605\RIVWARE;Database=RIVWARE;Integrated Security=true;"
Dim mySQLConnection As New SqlConnection(SQLConnectionString)
' Populate the list box using an array as DataSource.
mySQLConnection.Open()
Dim SQLDataTable As New System.Data.DataTable
'Create new DataAdapter
Dim mySQLDataAdapter = New SqlDataAdapter("SELECT * FROM [Rivware].[dbo].[RivetTypes]", mySQLConnection)
mySQLDataAdapter.Fill(SQLDataTable)
ListBox1.DataSource = SQLDataTable
ListBox1.DisplayMember = "RivetType"
' remove validation data from list
Dim Count As Integer
Dim X As Integer
'get the column of data to search for
For Count = 0 To ListBox1.DataSource.Columns.Count - 1
X = InStr(ListBox1.DataSource.Columns.Item(Count).ToString, "Name")
If X <> 0 Then Exit For
Next
' now search for any invalid rows, in that column. those containing "---"
Dim TheTable As DataTable = CType(ListBox1.DataSource, DataTable)
Dim theRow As DataRow() = TheTable.Select()
Dim RowNumber As Integer
For RowNumber = 0 To UBound(theRow) - 1
If theRow(RowNumber).Item(Count).ToString <> "---" Then
' data is OK so transer it to the other listbox
ListBox2.Items.Add(theRow(RowNumber).Item(Count - 1).ToString)
End If
Next
ListBox2.ClearSelected()
End Sub 'NewNew
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
TextBox1.Text = ListBox2.SelectedItem.ToString()
End Sub
End Class 'ListBoxSample3