0

I am trying to refresh a datagirdview when i add a new record using a dialog form. I would like to know how can i refresh my datagirdview. I have two Win Forms . Form A is called FrmContactDetailList which is having a datagridview which i is showing data from sql server. Below first block of code is used to bind data to the grid. which is given in form load event and also in this form i have a Button called "Add New Record" . Once i press this button its opening a win form which is opening another form. Below is that code which i used to open this in by button click event.

This will open Form B. Form is called FrmClientDetails. This form will have a text box and a save button . So once i enter the new name in the text box and press save i want the datagirdview which is in Form A to be updated . and show the new record once i close Form B. how can i achieve this.

This Code is used to bind the datagridview. I have given this is the form load event.

 Sub GetContactList()
        Dim BindData As New VoucherClass
        Dim dt As DataTable = BindData .Get_Client_List
        DataGridView.DataSource = dt 
 End Sub


 Private Sub FrmContactDetailsList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetContactList()
 End Sub

I have using this code to open the dialog form to enter the new data.

  Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click
        Dim FrmNewContact As New FrmClientDetails
        FrmNewContact.Owner = Me
        FrmNewContact.ShowDialog()
    End Sub
1
  • Binddata and dt are local variables. You dont supply them to the New form and my Guess is that the New record is Only added to the database. Which means that you Change nothing with the dgv yet you expect it to magically change when the form is closed. This is your problem. If not the we need more code. This is inconclusive.
    – WozzeC
    Dec 10, 2012 at 6:25

2 Answers 2

0

When the binding data change it is automatically reflected to the data grid view that is bond.

Edit:

Handle the FrmNewContact's Closing event. You can refresh your datagridview in that sub.

Dim WithEvents dialog As New FrmNewContact
Sub done() Handles dialog.FormClosed
    Me.DataGridView1.Refresh()
End Sub

Next edit:

Dim BindData As New VoucherClass
Dim dt As DataTable = BindData .Get_Client_List

Declare them outside of the sub. So you should have this:

Dim BindData as VoucherClass
Dim dt as DataTable

Sub GetContactList()
        BindData = New VoucherClass
        dt = BindData .Get_Client_List
        DataGridView.DataSource = dt
End Sub
4
  • Nop it does not change i need to close the form which is having the datagidview and open again then i get the changes updated . but i need this to happen without closing the form
    – IsseY
    Dec 7, 2012 at 17:39
  • I tried many ways but i am unable to do so . can you just show me some sample codes.
    – IsseY
    Dec 7, 2012 at 17:44
  • Please note i have given more explanation .pls help me
    – IsseY
    Dec 7, 2012 at 18:14
  • I have updated with the Form load even code. My Grid is in Form A . I have a text box and a button in Form B
    – IsseY
    Dec 7, 2012 at 18:54
0

Try this: Instead of FrmNewContact.ShowDialog()
If FrmNewContact.ShowDialog() = DialogResult.OK Then
Me.DataFridView.Refresh()
End IF
You may need to include a setter for your dialog result at the close action for your modal.
Me.DialogResult = DialogResult.OK
Me.Close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.