How to delete and clear the datagridview rows in vb 2010 - vb.net

I am developing a Point of Sale System in VB and using access as RDBMS. Everything went well however when it came to deletion i got stuck. I want to clear all the rows from the datagridview and want to delete it from database also. The new datagridview will store new items and i am sending that new data to print.I tried the below codes but all in vain.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
For i As Integer = 0 To SProductDataGridView.RowCount
SProductDataGridView.Rows.Remove(SProductDataGridView.Rows(0))
Next
End Sub
2nd Try
For i As Integer = 0 To SProductDataGridView.RowCount -1
ProductTableAdapter.DeleteProduct(IDTextBox.Text)
ProductTableAdapter.Fill(MyDS.product)
ProductTableAdapter.Dispose()
Next
Note : gridview.rows.clear() + gridview.ds = nothing , i tried that butgridview.rows.clear() doesn't work and gridview.ds = nothing just clear the gridview and is not deleting data from access database.

You can use either of below options:
Using DataTable
Using DataGridView
Using BindingSource
The logic of all solutions is removing the rows and then updating the table adapter.
Using DataTable
You can find each rows in the data table and delete it, then update the table adapter:
For Each row As DataRow In Me.TestDBDataSet.TestTable.Rows
row.Delete()
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)
Using DataGridView
For Each row As DataGridViewRow In Me.TestTableDataGridView.Rows
DirectCast(row.DataBoundItem, DataRowView).Delete()
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)
Using BindingSource
For Each item As Object In TestTableBindingSource
TestTableBindingSource.Remove(item)
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)

Related

vb.net insert data from listcheckbox to datagridview when an item checked and deleted when unchecked item

When I check a car make the car_model appear from database and when I check the car_model it's added automatically to datagridview1.
My question is when I try to check another car_make to add the new car_model of this car make to the DataGridView all the DataGridView items are deleted due to the (DataGridView1.Rows.Clear()) that I put in the code.
However, when I remove this comand the code duplicates every thing, can you help me solving this problem.
Private Sub acar_modelbox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles car_modelbox.SelectedIndexChanged
Dim itemChecked1 As Object
For Each itemChecked1 In car_makelistbox.CheckedItems
DataGridView1.Rows.Clear()
Next
Dim itemChecked2 As Object
For Each itemChecked2 In car_modelbox.CheckedItems
DataGridView1.Rows.Add(combobox1.SelectedItem("Id").ToString, (combobox1.SelectedItem("carvin").ToString, itemChecked1.item("car_make").ToString, itemChecked2.item("car_model").ToString)
Next
End Sub

Delete record from Access database vb.net

In this project I use Access database which is displayed in DataGridView. I am trying to delete acces row but i my looking for was not successful.
Code to delete record from DataGridView:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim index As Integer
index = DataGridView1.CurrentCell.RowIndex
DataGridView1.Rows.RemoveAt(index)
ZoznamBindingSource.RemoveCurrent().
‘Dim da As OledbDataAdapter
‘Dim ds As dataSet
da.Update(ds)
End Sub
The last line of code give me an error: SystemNullReferenceException. I know rhe dataset is problem but i don’t know which code will replace it with.
Any solution?
The whole point of a BindingSource is that it is the one and only point of contact for bound data. You shouldn't have to touch the UI and you shouldn't have to touch the data source.
In your case, you should be calling RemoveCurrent on the BindingSource. That will flag the underlying DataRow as Deleted and then, when you call Update on your data adapter, the corresponding database record will be deleted.

Connect Treeview Child Node to Database Details

I'm working with VB.net 2010 & have a Treeview that's being populated from a MySQL table via a Dataset in the Form1_Load.
The treeview loads the test data from the database table fine but I want to be able to click a treeview Child Node & display the matching data from the MySQL table Row inside multiple textbox.
I know I need to have a textbox for each table Cell in a Row but I can't figure out how to connect the table Row ID column to the treeview (child node). My table ID column is named UserID in the screenshot below.
I also know I need to add code to the TreeView1_AfterSelect. I think I might be able to use the treeview node Tag to connect to the table UserID column, but I'm just not sure about the code.
My treeview root node is set in design view.
I only have a Binding Navigator & Datagrid on the Form while testing the treeview/table-details, those will eventually be removed from the Form.
Here's some background on how I'm loading the treeview with my test data from the MySQL table.
Here's some screenshots of my Table data & the VB.NET Form, this site won't let me post images.
MySQL Table Data
VB.NET Form image
I've been trying to get this to work for at least a week with no luck. Any help would be very much appreciated.
Private Sub AddNode(parentNode As String, nodeText As String)
Dim node As New List(Of TreeNode)
node.AddRange(TreeView1.Nodes.Find(parentNode, True))
If Not node.Any Then
'' '' ''This is the Parent Treeview Node
node.Add(TreeView1.Nodes(0).Nodes.Add(parentNode, parentNode))
End If
'' '' ''This is the Child Treeview Node
node.Add(TreeView1.Nodes(0).Nodes(parentNode).Nodes.Add(nodeText, nodeText))
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Database1DataSet.Table44' table. You can move, or remove it, as needed.
Me.Table44TableAdapter.Fill(Me.Database1DataSet.Table44)
For Each dr As DataRow In Database1DataSet.Table44.Rows
AddNode(dr("ParentNode").ToString, dr("ChildNode").ToString)
Next
TreeView1.ExpandAll()
End Sub
Your database structure doesn't quite make sense to me. Can there be more than one city per state? Or is it always one user and one city. It's not clear.
Anyway, you can use the node's Key parameter to set the Name property, and see what you clicked on:
Private Sub AddNode(parentNode As String, nodeText As String, userID As String)
Dim node As New List(Of TreeNode)
node.AddRange(TreeView1.Nodes.Find(parentNode, True))
If Not node.Any Then
node.Add(TreeView1.Nodes(0).Nodes.Add(parentNode, parentNode))
End If
node.Add(TreeView1.Nodes(0).Nodes(parentNode).Nodes.Add(userID, nodeText))
End Sub
Then in your AfterSelect event, see if your key is a number:
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) _
Handles TreeView1.AfterSelect
Dim userID As Integer = 0
If Integer.TryParse(e.Node.Name, userID) Then
' do something with userID
End If
End Sub

Adding columns from sql table without cell values

I am currently working in windows form applications vb.net for desktop 2013. I am trying to copy a row of values from a parent datagridview to a secondary datagridview with a button. I have a code to copy the row but i need to fill the secondary datagridview with the column information before I can make the copy happen. The first datagridview is set up through an sql connection. Is there a way to load ONLY the columns with no cell values into the secondary datagridview?
Here is my copy code for the button.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Dim dr As New System.Windows.Forms.DataGridViewRow
For Each dr In Me.DataGridLine2.SelectedRows
Me.DGVOrdered.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value,
dr.Cells(2).Value)
Next
'IsDBNull(Me.DataGridLine2.Rows.Count)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
I figured out the code to copy the columns only, however now my problem is I'm getting an error message about trying to copy bound data.
Here is the code I used for future reference of anyone trying to do this.
dtselection.clear()
For columnindex As Integer = 0 To DataGridLine2.Columns.Count - 1
With dtselection.columns
.Add(DataGridLine2.Columns(columnindex).Name, DataGridLine2.Columns(columnindex).ValueType)
End With
Next
DGVOrdered.DataSource = dtselection

refreshing datagridview after populate existing datatable in vb.net

I am working on a windows form application using VB.net. I have populated a Datagridview1( dataset1.existingtable is the datatable). Now i wish to get distinct values from one column of its datatable and then populate another Datagridview2(dataset2.uniquerecords is the datatable).
PROBLEM: Not able to refresh data in Datagridview2 using design mode. However i am able to refresh data when dynamically creating a datatable at runtime.
The below sub is called after an event after my form has loaded completely.
The below code does NOT work
Private Sub loaddistinctrecords()
uniquerecords = existingtable.DefaultView.ToTable(True, "column_name")
Datagridview2.Refresh()
End Sub
The below code works
Private Sub loaddistinctrecords()
Dim newuniquerecords As New DataTable()
newuniquerecords = existingtable.DefaultView.ToTable(True, "column_name")
Datagridview2.DataSource = newuniquerecords.DefaultView
End Sub
well it looks like one cannot directly assign a datatable to another datatable and expect the datagridview to update automatically if the datatable was created from design mode.
What one can do is simply clear the existing records and then merge records from the source datatable.
Private Sub loaddistinctrecords()
uniquerecords.Clear()
uniquerecords.Merge(existingtable.DefaultView.ToTable(True, "table_name"))
End Sub