How to refresh a DataGridView when a dialog form is closed - sql

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

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

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

Related

frm.showDialog dispose when openFileDialog close [vb.net]

I have two forms. First form is used to display a set of record and second form is used to edit the particular record. I called the second form using frm.ShowDialog(). Inside that form I got a button to call the OpenFileDialog. When I press OK or Cancel, then the second form dispose together with the OpenFileDialog. I'm pretty should that my code is correct, but it was the ShowDialog() problem. Anyone have idea on this issue?
This is how i called the second form from the first form to display the Information.
Private Sub btnViewOrganizationEdit_Click(sender As Object, e As EventArgs) Handles btnViewOrganizationEdit.Click, dgvOrganization.DoubleClick
Dim selectedOrganization As New Organization
'check permission because double click
If dgvOrganization.RowCount > 0 Then
strOrganizationID = dgvOrganization.SelectedRows.Item(0).Cells(0).Value
selectedOrganization = helperOrganizationCKJ.getOrganizationByID(strOrganizationID)
frmEditOrganizationCKJ.objOrganization = selectedOrganization
frmEditOrganizationCKJ.ShowDialog()
iniGridView()
End If
End Sub
This is how i called the OpenFileDialog.
Private Sub btnEditOrganizationImage_Click(sender As Object, e As EventArgs) Handles btnEditOrganizationImage.Click
dlgImage.Filter = ""
Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
Dim sep As String = String.Empty
For Each c As ImageCodecInfo In codecs
Dim codecName As String = c.CodecName.Substring(8).Replace("Codec", "Files").Trim()
dlgImage.Filter = String.Format("{0}{1}{2} ({3})|{3}", dlgImage.Filter, sep, codecName, c.FilenameExtension)
sep = "|"
Next
dlgImage.FilterIndex = 5
If dlgImage.ShowDialog(Me) = DialogResult.OK Then
'Get the image name
Dim img = dlgImage.FileName
picEditOrganizationImage.Image = System.Drawing.Bitmap.FromFile(img)
End If
End Sub
The frmEditOrganizationCKJ just dispose together with the dispose of OpenFileDialog.
Probably you have copy/pasted your btnEditOrganizationImage from a button that has the DialogResult property set to something different than DialogResult.None.
This triggers the closing action for your modal form and the fix is really simple.
Set the property DialogResult for the btnEditOrganizationImage to DialogResult.None
From MSDN on Button.DialogResult
If the DialogResult for this property is set to anything other than
None, and if the parent form was displayed through the ShowDialog
method, clicking the button closes the parent form without your having
to hook up any events. The form's DialogResult property is then set to
the DialogResult of the button when the button is clicked

Display A Picture From Database

I have a datagridview and when I click inside, it opens another form with the information that is inside the datagridview.
In my Access database I have OLE Object with a picture (bitmap) and I want to display that in the form info too.
This is the code that I have to get the info when I click in the datagridview:
Private Sub DataGridView1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
Dim curForm As Form = New Form
With curForm
info.TextBox1.Text = DataGridView1.SelectedRows(0).Cells(1).Value
info.TextBox2.Text = DataGridView1.SelectedRows(0).Cells(2).Value
info.TextBox3.Text = DataGridView1.SelectedRows(0).Cells(3).Value
info.TextBox4.Text = DataGridView1.SelectedRows(0).Cells(4).Value
info.TextBox5.Text = DataGridView1.SelectedRows(0).Cells(5).Value
info.Show()
End With
End Sub

VB.Net Add value from datagridview to listbox in another form

I have a trouble on VB.Net desktop programming, I have a Form with a ListBox, Called Form1, this form have a button that will appear another form with DataGridView inside that new Form, we will call this as Participant. Then I have a scenario, when User click the Button, it will call the Participant Form, and when user click the datagridview row in the Participant Form, it will get the value from that row and add the value to the ListBox in Form1, I have done getting the value from the row, but I can't add the value to the ListBox. This is my code to help you understand what I have done.
This is the code that will call the Participant Form :
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
Dim myForm As New Participant
If myForm Is Nothing Then
myForm = New Participant
End If
myForm.Show()
End Sub
And this is the code that will add the value from DataGridView in Participant Form to the ListBox in Form1
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 1
If e.ColumnIndex = yourColumnIndex Then
participant_Share = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Form1.ListBox1.Items.Add(participant_Share)
End If
End Sub
But this code cannot insert the value to the ListBox, Please help me if you know how to solve this.
Thank you
You can change Participant constructor to accept a ListBox as parameter and store it in a local variable.
Private yourListBoxReference As ListBox
Public Sub New(ByVal yourListBox As ListBox)
InitializeComponent()
yourListBoxReference = yourListBox
End Sub
So you can call this form as:
myForm = New Participant(yourListBox)
And use the local variable to add an element to your ListBox:
yourListBoxReference.Items.Add(participant_Share)

Remove the windows form controls on exit

I'm adding the form controls on loading the form manually:
Me.FieldI = New TextBox()
Me.FieldI.Location = New System.Drawing.Point(50, 10)
Me.FieldI.Name = "FieldI"
Me.FieldI.Size = New System.Drawing.Size(40, 20)
Me.FieldI.TabIndex = 5
Me.Conversion.Controls.Add(Me.FieldI)
[..]
When I close the form window and reopen it, the control is still there (with the old .Text content , because its an textbox in this case).
I would like to remove the controls that have been created while form loading on the form close event, to prevent doubling the elements on my form.
How can I achieve this?
edit
Form closing code looks following (just showing up the main form back):
Private Sub Form1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing
Main.Show()
End Sub
The problem here is that the form is not being disposed, so when you open it again the controls are still there from the last time it was opened.
Try the following:
Using frm = New subForm()
frm.ShowDialog()
End Using
The variable frm will be disposed after the using.
Also...
You can also provide feedback from a dialog, to check whether the form was successful or not. For example:
Dim frm As New subForm()
If frm.ShowDialog = DialogResult.OK Then
'YAY!
Else
'Something failed
End If

combobox not being populated

I have a windows form project with a main form. There is a textbox leave event that opens a new form. In that new forms load event i have a combobox item loop that populates the combobox items. It works perfectly fine if run on the main form but doesnt work on the second form. Why doesn't the comboboxes on the secondary form populate when that form is opened via a textbox_leave event from the main form?
this is the leave event
Private Sub tbChartTitle_Leave(sender As Object, e As System.EventArgs) Handles tbChartTitle.Leave
If Not tbChartTitle.Text = Nothing Then
frmTitleAttributes.Show()
End If
End Sub
This is the code that populates one of the comboboxes on the second form (it works if run on a combobox on the main form)
Private Sub frmTitleAttributes_Load(sender As Object, e As System.EventArgs) Handles Me.Load
InitializeComponent()
AddFonts()
End Sub
Private Sub AddFonts()
' Get the installed fonts collection.
Dim allFonts As New Drawing.Text.InstalledFontCollection
' Get an array of the system's font familiies.
Dim fontFamilies() As FontFamily = allFonts.Families
' Display the font families.
For i As Integer = 0 To fontFamilies.Length - 1
cbxTitleFonts.Items.Add(fontFamilies(i).Name)
Next
End Sub
make sure that the Load handler is hit after you show your form (use break point)
also you can try to call it in the Shown event
Private Sub frmTitleAttributes_Shown(sender as Object, e as EventArgs) _
Handles frmTitleAttributes.Shown
AddFonts()
End Sub