Update Listbox after inserting a row in database - vb.net

I have a listbox which has a bindingsource which connects to access database. have following code which creates and adds new line to my access database and to datagrid view but changes are not updating in the listbox.
Dim drv As DataRowView = DirectCast(EQtblBindingSource.AddNew(), DataRowView)
drv.BeginEdit()
drv.Row.BeginEdit()
drv.Row("eiD") = "SS"
drv.Row("EQ_NAME") = "DUMMY"
drv.Row.EndEdit()
drv.DataView.Table.Rows.Add(drv.Row)
EQ_tblTableAdapter.Update(EQDATADataSet.EQ_tbl)
EQtblBindingSource.ResetBindings(True)
Is there a way to reflect changes immediately after i add a new row? resetbindings seems not working or another option to reload or refresh my listbox. Any idea please help.
Thanks

Make sure that the DataSource for your listbox is set to the bindingsource.
Your code can be simplified to this:
With EQtblBindingSource
.AddNew()
DirectCast(.Current, DataRowView)("eiD") = "SS"
DirectCast(.Current, DataRowView)("EQ_NAME") = "DUMMY"
.EndEdit()
End With
EQ_tblTableAdapter.Update(EQDATADataSet.EQ_tbl)

Related

How to filter datagridview by string in textbox in visual basic without using SQL query WHERE?

I'm trying to filter rows of datagridview based on textBox value. I want to remove all rows which don't have values like in column NAZIV. I'm new in visual basic.
Name of datagridView is dvgIQ
I've tried this but it's not working.
Sub filter
If textBox1.Text.Length>=3 Then
For i As Integer = dvgIQ.Count-1 To 0 Step -1
If Not dvgIQ.Rows(i).Cells(4).Value.ToString(textBox1.Text.ToLower) Then
dvgIQ.Rows.RemoveAt(i)
End If
Next i
Else If textBox1.Text.Length>0 And textBox1.Text.Length<3 Then
MsgBox("warning")
End If
End Sub
Thanks in advance
You should start by populating a DataTable with your data. If the data comes from a database then use a data adapter and call its Fill method or else use a data reader and call Load on the DataTable. If the data is not from a database then you can build and populate the DataTable manually.
Next, bind the Datatable to a BindingSource that you added to the form in the designer. Finally, bind the BindingSource to your grid:
BindingSource1.DataSource = myDataTable
DataGridView1.DataSource = BindingSource1
To filter the grid, you simply set the Filter property of the BindingSource, e.g.
BindingSource1.Filter = $"SomeColumn LIKE '{TextBox1.Text}%'"
That will exclude all rows where SomeColumn does not start with the text in TextBox1. You should read the documentation for the Filter property and follow the specified link to learn what syntax is supported by ADO.NET. It is a small subset of SQL.

Updating Datatable as Datasource

I'm using a data table as a data source for a data grid in VB.NET.
Public Property InterimLotTable As DataTable
uxDataGridAuditSamplerView.DataSource = SamplerParent.InterimLotTable
But when I update the SamplerParent.InterimLotTable its not reflected in my dataGrid.
Here is the code where I update the Datatable.
Dim sql = String.Format("select * from CAMS.VW_BL_TAS_InterimLotData where PARENTLOT = '{0}'", ParentLot)
Dim dynaset = db.CreateDynaset(sql, DBWrapper.DynasetOptions.ORADYN_READONLY)
InterimLotTable = dynaset.CopyToDataTable()
I thought that with the databinding when I made changes to the InterimLotTable the datagrid would be updated automatically.
You're not updating InterimLotTable, you're replacing it - i.e. the property now returns a different object.
The controls that are bound to the old object don't know anything has happened, as the object they have a reference to, hasn't changed.
You could use an intermediate BindingSource, or raise an event so the UI can choose to rebind as appropriate

Showing a single record in datagridview from access database vb.net

What I have is a small application that has a datagridview and an Access DB. There are 10 records in the database and they all show up in the datagridview when the app is launched. I need to be able to do two things:
I need to be able to enter an id number in a textbox and have JUST that record show in the DGV.
I need to be able to have it show all the records again (using a different click event of course).
I am able to choose the record with this:
Dim row As DataRow
row = ProductsDataSet.tblProducts.FindByItemNum(txtNumber.Text)
but I am unable to figure out how to show just that record in the DGV.
(Let me clarify...I THINK I am choosing the record simply because no exceptions are being thrown).
Any guidance here would be greatly appreciated. Thank you.
John
An option is to set the dataSource of your dataGridView to the data you'd like to display. Below is an implementation using dataTables.
Class MyForm
Dim dataTableAll, dataTableOneID, As DataTable
Sub show1ID_Click
Dim id As Integer = InputBox("Enter ID")
dataTable2 = dataTable1.Select("[ID] = id").CopyToTable
dataGridView.DataSource = dataTableOneID
End Sub
Sub showAll_Click
dataGridView.DataSource = dataTableAll
End Sub
End Class

Refreshing Datagridview From Dataset Issue

I seem to be pulling my hair out over something that seems pretty straight forward in my eyes. I cannot get a datagridview control to update correctly after altering a dataset.
I initially populate a datagridview from a dataset and auto generate the columns no problem. However, if I regenerate my dataset with new data, the datagridview will display the new data correctly, but won't remove the old column headers.
For example, I have a query that gives me Store Name and Manager and auto populates my datagridview. Then if I change my query to just give me Store Name and Supervisor, it gives me Store Name, Manager (with blank column) and Supervisor.
I have tried datagridview.refresh() and datagridview.update() but nothing seems to work.
Here is my code:
MySQLConn.Open()
Dim ExQry As New MySqlCommand(QryStr, MySQLConn)
ExQry.CommandType = CommandType.Text
Dim da As New MySqlDataAdapter(ExQry)
dasCustomQryData.Clear() 'my dataset is called dasCustomQryData
da.Fill(dasCustomQryData, "QryData")
da.Update(dasCustomQryData, "QryData")
With dgvCustomQuery
.DataSource = Nothing
.Dock = DockStyle.Fill
.AutoGenerateColumns = True
.DataSource = dasCustomQryData
.DataMember = "QryData"
.Refresh()
.Visible = True
End With
MySQLConn.Close()
da.Dispose()
dasCustomQryData.Dispose()
So, when I want to update my datagridview, I plugin a new QryStr to the above code and it rebuilds my dataset. The dataset is getting updated, because the datagridview contains the correct data, however, my problem is that the datagridview isn't clearing the old columns that aren't populated anymore.
All help appreciated. Thanks
I would recommend you create a connection and a dataset straight from VB and the in the dataset it shows the tables. At the bottom of the tables is a standard query but u can create your own...with variables as parameters and then you can call the query through the table adapter and assign this straight to the datagridview.
That always seems to work for my programs. What sort of database are u using? Might be obvious but just to make sure, the datagridview is not bound right? Sample from one of my projects dgData.DataSource = TblEventsTableAdapter.ViewEvent(cmbEvents.SelectedItem.ToString) where dgdata is obviously datagridview. ViewEvent is a custom query created in the dataset and takes in 1argument. Hope this is of use to you
Ok, after a bit of troubleshooting and playing around with code, I found that the simple solution to this was that the datatable itself was not releasing the column headers. So even though the datagridview and the dataset was getting cleared, the datatable was still populated.
The following command solved my issue:
dt.Columns.Clear()

set datasource of gridview to a datatable without bind

I am seting a datasource of a dataGridview to a table.
First In Load Event
Datagridview.datasource=DTFromSQl
In some Random Event I do.
Dim Dt as datatable=DataGridview1.datasource
When I do DT.rows.clear() it also clears the rows in the Datagridview. I suppose its due to databinding. But how is the databind occuring? and how to remove it so that changes in DT occurs in it only.
Thanks
Ok I did it but, something doesnt feel right about it.
Dim DTSend As New DataTable
For i As Integer = 0 To DataGridView1.ColumnCount - 1
DTSend.Columns.Add(DataGridView1.Columns(i).Name)
Next
Use the following code. when you will clear the datatable it will not clear your datagridview
Dim dtsend As DataTable
dtsend = CType(DataGridView1.DataSource, DataTable).Copy()
dtsend.Clear()