set datasource of gridview to a datatable without bind - vb.net

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()

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.

Update Listbox after inserting a row in database

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)

Open Form faster & remove "System.Data.DataRowView" flicker from Combobox data binding

I have plenty Comboboxes on my form (around 20), and all of them are displaying items from different tables of my DB. If I put all code on Form_Load event then Form opens very slow. So I tried to paste code in different varieties, and currently I'm stuck at Combobox_Enter event - now Form loads fast, but when I click on drop-down of combobox I see sometimes "System.Data.DataRowView" flickering before items are loaded in Combobox. Is there any way to achieve both - fast Form opening & Combobox loading Items without flickering ?....So far I tested with Form_Activate,Form_GotFocus(not working) and Combobox_GotFocus,Combobox_MouseHover,Combobox_Click(not exactly perfect). This is an example of how I bind my Comboboxes:
Private Sub Combobox1_Enter(sender As Object, e As EventArgs) Handles Combobox1.Enter
Dim SQL As String = "SELECT Name from MyTable"
Dim dtb As New DataTable()
Using con As OracleConnection = New OracleConnection("Data Source=MyDB;User Id=Lucky;Password=MyPassword;")
Try
con.Open()
Using dad As New OracleDataAdapter(SQL, con)
dad.Fill(dtb)
End Using
Combobox1.DataSource = dtb
Combobox1.DisplayMember = "Name"
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
Combobox1.SelectedIndex = -1
End Using
End Sub
I also tried with declaring " Public con As OracleConnection", but output is same as I have It now.
Any help appreaciated !
When binding a ComboBox or the like, you should pretty much ALWAYS set the DataSource last. You are not and that's why you see "System.Data.DataRowView" displayed.
When you bind a list to a ComboBox, the control will display data from the column or property specified in the DisplayMember if there is one, otherwise it will call ToString on each item. In your code, you first set the DataSource and, at that point, the DisplayMember is not set so the control calls ToString on each item. The result of that is "System.Data.DataRowView". When you then set the DisplayMember, those values that the control just went to the trouble of generating and displaying are discarded and the DisplayMember is used to get new values.
Even if you weren't seeing that effect, you'd still be wasting your control's time generating values that you don't want. ALWAYS set the DisplayMember, ValueMember or the like before setting the DataSource in code unless you have a specific reason not to. The only reason that I'm aware of is when you're binding a CheckedListBox, which has an issue when DataSource is set last.
By the way, shouldn't you have a test there to only retrieve data if there is no data already loaded? You don't want to reload data if the user returns to the same control, do you?

Add row to datagridview in vb.net

When I try add an extra row to my datagridview I get following error:
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
Any idea to fix this, without databinding I added rows like this:
' Populate the rows.
Dim row() As String = {omschrijving, aantalstr, eenheidsprijs, basisbedrag, kortingstr, kortingbedrag, netto, btw, btwbedrag, totaal, productid}
DataGridView1.Rows.Add(row)
It looks like your grid view is bound to a data object. In that case, you need to add the row to the object it is bound to, like a dataset.
For instance, a rough example would be:
Dim boundSet As New DataSet
Dim newRow As DataRow = boundSet.Tables(0).NewRow
With newRow
.Item(0) = "omschrijving"
.Item(1) = "aantalstr"
...
End With
boundSet.Tables(0).Rows.Add(newRow)
boundSet.AcceptChanges()
You would just need to use the dataset that was bound to your grid view instead of creating a new one.

Refresh a DataGridView on update to its list data source

In my current implementation I'm using a data grid view that is fed from a list of objects.
I'm trying to add new entries, by adding new objects to the list.
I'm finding that when these new entries are added to the list, they do not appear in the DataGridView. I've tried updating it, clearing it and refilling it. All to know avail.
Any help would be greatly appreciated.
This was solved by using a binding source like so.
Dim bs As New BindingSource
bs.DataSource = Entries
DataGridView1.DataSource = bs
And then reassigning the data source each time.
Public Sub Grid_Update()
bs = New BindingSource
bs.DataSource = Entries
DataGridView1.DataSource = bs
End Sub
Re assign grid source to updated list of objects and call databind method again.