I have a form with DataGridView, dataSourced by dataTable. When i insert the record at the end i press the Clear button or form all fields (textboxes, comboboxes, DGV checkboxes) get cleared automatically when i press the save button.
I normally clear the textboxes and comboboxes by writing:
textbox1.text = ""
combobox1.text = ""
but when i do clear DGV with this like:
dgvPurchase.dataSource = nothing
dgvPurchase.Refresh()
or dgvPurchase.rows.clear()
It does clear the DGV but when i add new entry so DataGridView and data both do not appear into DGV. On the Form Load event I have the following code:
dtField = retrieveFull("ProductBasicInfo")
cmbPurProdID.DisplayMember = "ProdName"
cmbPurProdID.ValueMember = "ProdID"
cmbPurProdID.DataSource = dtField
dtPurchase.Columns.Add("ProdID")
dtPurchase.Columns.Add("ProdName")
dtPurchase.Columns.Add("RetailerID")
dtPurchase.Columns.Add("RetailerName")
dtPurchase.Columns.Add("UnitPrice")
dtPurchase.Columns.Add("Qty")
dtPurchase.Columns.Add("Amount")
dgvPurchase.DataSource = dtPurchase
Please guide me that is there any way that my DataSource do not disturb or if I clear it so it also refresh the DataSource.
This is the add button code, which adds the data into DataGridView
Try
For a As Integer = 0 To dtPurchase.Rows.Count - 1
If dtPurchase.Rows(a).Item("ProdID") = cmbPurProdID.SelectedValue Then
MessageBox.Show("This product is already enlisted")
Exit Sub
End If
Next
dRow = dtPurchase.NewRow
dRow.Item("ProdID") = cmbPurProdID.SelectedValue
dRow.Item("ProdName") = cmbPurProdID.Text
dRow.Item("RetailerID") = cmbPurRetailerID.SelectedValue
dRow.Item("RetailerName") = cmbPurRetailerID.Text
dRow.Item("UnitPrice") = txtPurUnitPrice.Text.Trim
dRow.Item("Qty") = txtPurQty.Text.Trim
dRow.Item("Amount") = txtPurAmount.Text.Trim
dtPurchase.Rows.Add(dRow)
datagridview'sDataSourceto new emptyDataTource. - MarekForm.Loadto own method (for example:LoadData). Then call this method inForm.Loadevent handler and after you emptying rows... - Fabio