Real Time Updating winforms visual studio 2010 .net - vb.net

I am having some problems getting the data to reload after its updated to the database i am loading my gridview from form load BindGrid() event I have included my code here.
My Delcarations are as follows I have tried everything here but cant force it to refresh the grid
Dim dbContext As New R3Delivery
Dim threeContext As New skechersDeliveryEntities1
Dim bs As New BindingSource
Private Sub frmConfirmDeliverys_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
BindGrid()
dgDeliverys.Columns(0).ReadOnly = True
dgDeliverys.Columns(1).ReadOnly = True
dgDeliverys.Columns(2).ReadOnly = True
End Sub
Public Sub BindGrid()
Dim cfglocation As Int16
cfglocation = cfb.StoreLocation
bs.DataSource = (From u In threeContext.R3Delivery Where u.isprocessed = True AndAlso u.location = 1
Select u)
bs.ResetBindings(True)
dgDeliverys.DataSource = bs
End Sub
My save button is as follows
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
threeContext.SaveChanges()
BindGrid()
End Sub
I thought I should show my declaration of my form as well the above code is in my edit form the below is my calling from
Dim frmConfirmDeliverys As New frmConfirmDeliverys
frmConfirmDeliverys.ShowInTaskbar = False
frmConfirmDeliverys.ShowDialog()

have you tried doing this?
dgDeliverys.Datasource=Nothing
dgDeliverys.DataSource = bs
or
dgDeliverys.Refresh
Also like jmcilhinney said, check if the data you are expecting is returned by the query.

Related

UI is laggy when using serial communication

Good day!
I want to read data from serial port. The data can be read every 2 secs and save it to a database, and luckily did it. I used a datagridview to display and save it. But it seems very laggy. I must wait for a few seconds to be able to click another button.
Are there other ways on how to do this?
Any help would do. Thanks!
Imports System.Data.OleDb
Public Class PICtoVB[enter image description here][1]
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Recto D Sanchez Jr\Desktop\Datalogger GUI\test\test\bin\Debug\database1.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Dim timerval As Integer = 0
Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconnect.Click
btnconnect.Enabled = False
btnread.Enabled = True
btndisconnect.Enabled = True
SerialPort1.Open()
Timer1.Enabled = True
End Sub
Private Sub btndisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisconnect.Click
btndisconnect.Enabled = False
btnread.Enabled = False
btnconnect.Enabled = True
Timer1.Enabled = False
SerialPort1.Close()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
timerval = timerval + 1
Dim serialdata1 As String
Dim serialdata2 As String
Dim serialdata3 As String
Dim serialdata4 As String
Try
serialdata1 = SerialPort1.ReadLine.ToString
serialdata2 = SerialPort1.ReadLine.ToString
serialdata3 = SerialPort1.ReadLine.ToString
serialdata4 = SerialPort1.ReadLine.ToString
dgvdata.Rows.Add(serialdata1, serialdata2, serialdata3, serialdata4)
DATALOGBindingSource1.AddNew()
DATETextBox.Text = serialdata1
TIMETextBox.Text = serialdata2
TEMP__C_TextBox.Text = serialdata3
RH____TextBox.Text = serialdata4
DATALOGBindingSource1.EndEdit()
DATALOGTableAdapter1.Update(Database1DataSet1.DATALOG)
Catch ex As Exception
End Try
End Sub
You're reading data from the SerialPort on the UI thread, which means that it is too busy to maintain the UI, hence the UI freezing for periods. Generally speaking, you handle the DataReceived event of the SerialPort and read data there. That event handler is executed on a secondary thread, which means that you can read and manipulate data as much as you want without affecting the UI. You can't update the UI from there though, so once you have processed the data and are ready to display it, you need to marshal a method call to the UI thread and do the updating there. That's a topic you can find lots of examples for.

Strange behavior when setting null value in bound ComboBoxes

I prepared a small project demonstrating my issue. It consists of two data tables with a parent-child relation. I use vb.net 2010 with framework 4.0.
- Start a new WinForm project.
- On Form1 designer, drag one BindingNavigator, one Button and two ComboBoxes.
- In code behind, copy code below:
Public Class Form1
Private dsMain As DataSet
Private WithEvents bndSource As New BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dsMain = New DataSet
'Create tables
Dim dtContacts As New DataTable("Contacts")
Dim col As DataColumn = dtContacts.Columns.Add("IDContact", GetType(Integer))
col.AllowDBNull = False
col.AutoIncrement = True
col.Unique = True
dtContacts.Columns.Add("FullName")
col = dtContacts.Columns.Add("IDCountry", GetType(Integer))
col.AllowDBNull = True
dsMain.Tables.Add(dtContacts)
Dim dtCountries As New DataTable("Countries")
col = dtCountries.Columns.Add("IDCountry", GetType(Integer))
col.AllowDBNull = False
col.AutoIncrement = True
col.Unique = True
dtCountries.Columns.Add("A2ISO")
dtCountries.Columns.Add("CountryName")
dsMain.Tables.Add(dtCountries)
'Add relation
Dim rel As New DataRelation("rel", dtCountries.Columns("IDCountry"), dtContacts.Columns("IDCountry"))
dsMain.Relations.Add(rel)
'Populate parent table
dtCountries.Rows.Add(1, "AF", "Afghanistan")
dtCountries.Rows.Add(2, "AL", "Albania")
dtCountries.Rows.Add(3, "DZ", "Algeria")
'Populate child table
dtContacts.Rows.Add(1, "First Contact", 3)
dtContacts.Rows.Add(2, "Second Contact", 1)
'Set bindings
bndSource.DataSource = dtContacts.DefaultView
BindingNavigator1.BindingSource = bndSource
ComboBox1.DataSource = dtCountries
ComboBox1.DisplayMember = "A2ISO"
ComboBox1.ValueMember = "IDCountry"
ComboBox1.DataBindings.Add("SelectedValue", bndSource, "IDCountry")
ComboBox2.DataSource = dtCountries
ComboBox2.DisplayMember = "CountryName"
ComboBox2.ValueMember = "IDCountry"
ComboBox2.DataBindings.Add("SelectedValue", bndSource, "IDCountry")
dsMain.AcceptChanges()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.Text = Nothing 'Set IDCountry to DBNull, same as ComboBox1.SelectedIndex = -1
ComboBox2.Text = Nothing
End Sub
End Class
- Run the project.
- Try to navigate back and forth, and change items in the ComboBoxes: all is correct.
- Now set IDCountry = Null in Contacts table, as it is allowed. To achieve this, click on Button1.
- In ComboBox1, if you select the same item as it was before clicking the button, you see the behavior is not correct anymore: ComboBox2 doesn't update accordingly with ComboBox1 but remains empty.
- If you select another item, from now on all is correct again.
Is this a bug in .net data binding? If so, is there any workaround?
Thanks in advance.
This is the workaround I found. I didn't find the reason of the behavior I observed, but this code works for me:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex < ComboBox2.Items.Count Then ComboBox2.SelectedIndex = ComboBox1.SelectedIndex
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
If ComboBox2.SelectedIndex < ComboBox1.Items.Count Then ComboBox1.SelectedIndex = ComboBox2.SelectedIndex
End Sub
It simply makes ComboBox selected indexes equal.
Thank you anyway.

Datagridview and access database only updates after clicking a different row

I am reading an access database and populating the info in datagridview. My form has a DGV, and 3 buttons.
Button one copies the selected row to a datetimepicker control.
Button two copied the updated datetimepicker value back to the DVG
Button three does an update (writes the info back to the database).
My issue is that the info only gets updated in the database if I select a different row before hitting button three. I am not getting any error message in either case.
Below is my code. The database only has 2 columns (name and DOB - which is date/time).
Public Class Form1
Dim dbConn As New OleDb.OleDbConnection
Dim sDataset As New DataSet
Dim sDataAdapter As OleDb.OleDbDataAdapter
Dim sql As String
Dim iTotalRows As Integer
Dim sShipTypeFilter As String
Dim sBuildingFilter As String
Dim sCustSuppFilter As String
Dim sStatusFilter As String
Dim sDayFilter As String
Dim dv As New DataView
Sub myDBConn()
dbConn.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\terry\Documents\Database1.accdb"
Debug.Print("Start:" & DateAndTime.Now.ToString)
dbConn.Open()
sql = "select * from TableX"
sDataAdapter = New OleDb.OleDbDataAdapter(Sql, dbConn)
sDataAdapter.Fill(sDataset, "MyTable")
dbConn.Close()
iTotalRows = sDataset.Tables("MyTable").Rows.Count
Debug.Print("Rows from Access:" & iTotalRows)
Debug.Print("End:" & DateAndTime.Now.ToString)
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Call myDBConn()
Debug.Print("DVG1 row count before binding:" & DataGridView1.Rows.Count)
'dv = New DataView(sDataset.Tables(0), "Shipment = 'Regular' and Building = 'CSE'", "Company DESC", DataViewRowState.CurrentRows)
dv = sDataset.Tables(0).DefaultView
Debug.Print("DataView count:" & dv.Count)
DataGridView1.DataSource = dv
Debug.Print("DVG1 Rows:" & DataGridView1.Rows.Count)
DataGridView1.Columns("DOB").DefaultCellStyle.Format = "hh:mm tt"
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
dtp1.Value = DataGridView1.SelectedRows(0).Cells("DOB").Value
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
DataGridView1.SelectedRows(0).Cells("DOB").Value = dtp1.Value
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Debug.Print("switched row")
Me.Visible = False
Dim sqlcb As New OleDb.OleDbCommandBuilder(sDataAdapter)
sDataAdapter.Update(sDataset.Tables("MyTable"))
Me.Close()
End Sub
End Class
Before updating you need to Endedit. This means you need to add Endedit for the datagridview.
So this will be your code:
Debug.Print("switched row")
Me.Visible = False
Dim sqlcb As New OleDb.OleDbCommandBuilder(sDataAdapter)
Datagridview1.EndEdit()
sDataAdapter.Update(sDataset.Tables("MyTable"))
Me.Close()
EDIT1:
Dim dt As New DataTable
dbConn.Open()
sDataset.Tables.Add(dt)
sDataAdapter = New OleDbDataAdapter("Select * from TableX", dbConn)
sDataAdapter.Update(dt)
dbConn.Close()
I figured it out - thanks Stef for putting me on the right track.
My DGV is only updated programmatically (not by user edits) so I updated the code for button 2 to set the editmode, begin editing, update the selected DVG row and end editing:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
DataGridView1.BeginEdit(True)
DataGridView1.SelectedRows(0).Cells("DOB").Value = dtp1.Value
DataGridView1.EndEdit()
End Sub
After doing this modification - my datadapter.update command works!!

Public variable used for form opening not feeding through to from

Having some issues getting a form to populate based on a variable determined in current form.
I have a search result form that has a datagrid with all results, with an open form button for each row. When the user clicks this, the rowindex is used to pull out the ID of that record, which then feeds to the newly opened form and populates based on a SQL stored procedure run using the ID as a paramter.
However, at the moment the variable is not feeding through to the form, and am lost as to why that is. Stored procedure runs fine if i set the id within the code. Here is my form open code, with sci
Public Class SearchForm
Dim Open As New FormOpen
Dim data As New SQLConn
Public scid As Integer
Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sql As New SQLConn
Call sql.SearchData()
dgvSearch.DataSource = sql.dt.Tables(0)
End Sub
Private Sub dgvSearch_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSearch.CellContentClick
Dim rowindex As Integer
Dim oform As New SprinklerCardOpen
rowindex = e.RowIndex.ToString
scid = dgvSearch.Rows(rowindex).Cells(1).Value
TextBox1.Text = scid
If e.ColumnIndex = 0 Then
oform.Show()
End If
End Sub
End Class
The form opening then has the follwing:
Private Sub SprinklerCard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Populate fields from SQL
Try
Call Populate.SprinklerCardPopulate(ID)
cboInsured.Text = Populate.dt.Tables(0).Rows(0).Item(1)
txtAddress.Text = Populate.dt.Tables(0).Rows(0).Item(2)
txtContactName.Text = Populate.dt.Tables(0).Rows(0).Item(3)
txtContactPhone.Text = Populate.dt.Tables(0).Rows(0).Item(4)
txtContactEmail.Text = Populate.dt.Tables(0).Rows(0).Item(5)
numPumps.Value = Populate.dt.Tables(0).Rows(0).Item(6)
numValves.Value = Populate.dt.Tables(0).Rows(0).Item(7)
cboLeadFollow.Text = Populate.dt.Tables(0).Rows(0).Item(8)
cboImpairment.Text = Populate.dt.Tables(0).Rows(0).Item(9)
txtComments.Text = Populate.dt.Tables(0).Rows(0).Item(10)
Catch ex As Exception
MsgBox(ex.ToString & "SCID = " & ID)
End Try
End Sub
Set the ID variable in the form before you open it.
If e.ColumnIndex = 0 Then
oform.ID = scid
oform.Show()
End If

RightToLeft RichTextBox with pre-loaded text

Ok i found a strange type of bug in MS default Richtextbox in vb.net 2008. If we add some line of text in Richtextbox programmaticlly. there is a gape from right side. see the image below
here is my code
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form
Dim rtb As New RichTextBox
f.Width = 500
f.Height = 500
rtb.RightToLeft = Windows.Forms.RightToLeft.Yes
For i = 1 To 20
rtb.AppendText("بسم اللہ الرحمن الرحیم" & vbNewLine)
Next
rtb.Dock = DockStyle.Fill
f.Controls.Add(rtb)
f.Show()
End Sub
I can't explain it, but try changing the order of your code so that the RichTextBox control is added to the form before you append the text. This worked for me:
Private Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn1.Click
Dim f As New Form
f.Width = 500
f.Height = 500
Dim rtb As New RichTextBox
rtb.Name = "rtb"
rtb.Dock = DockStyle.Fill
rtb.RightToLeft = RightToLeft.Yes
f.Controls.Add(rtb)
For i = 1 To 25
rtb.AppendText("بسم اللہ الرحمن الرحیم" & vbNewLine)
Next
f.Show()
f.BeginInvoke(New Action(Of RichTextBox)(AddressOf RunFix), rtb)
End Sub
Sub RunFix(ByVal rtfControl As RichTextBox)
rtfControl.Select(0, 0)
rtfControl.ScrollToCaret()
End Sub
I added a hack BeginInvoke method that performs a ScrollToCaret() call which seems to fix the problem.
If you also set
rtb.Width = 500
rtb.Height = 500
Then it works as desired.
I agree it is strange behaviour. It does the same with VS 2012 RC.