0
votes

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)
1
Set the datagridview's DataSource to new empty DataTource. - Marek
Please add your form's screenshot. - Neolisk
Fast solution: Put code from Form.Load to own method (for example: LoadData). Then call this method in Form.Load event handler and after you emptying rows... - Fabio
@Marek: I have tried DGV.Datasource = NOthing but not working too. Neolisk I tried to add an image but i don't know that how i attach image to my thread. - user87

1 Answers

0
votes

You can create new empty DataTable and set it like this:

c#

DataTable emptyDT = new DataTable();
dgvPurchase.DataSource = emptyDT

vb.Net

Dim emptyDT As New DataTable()
dgvPurchase.DataSource = emptyDT

Does this work for you?