Hi,
I have two forms.Both having DatagridView.There are two comboboxes to filter the record from Database and fill in Datagridview.My requirement is when all the data is populated in DataGrid after selecting the comboboxes and when the user clicks on Delete.
The comboboxes and Datagrid on second form should have the same values as that was on first form.The names of my Comboboxes and Datagridview are same on both the forms
Below is the code my Datagrid view getting populated in First Form.

Sub GetData(ByVal StrQuery As String)
        If CBMedium.Text <> "" And CBClass.Text <> "" Then
            DGVStudRecord.Rows.Clear()
            con = DBConnect()
            cmd = New SqlCommand(StrQuery, con)
            dr = cmd.ExecuteReader
            If dr.HasRows Then
                While dr.Read
                  
                    DGVStudRecord.Rows.Add(dr(0), dr(1), dr(2), dr(3), dr(4), dr(5))
                End While
            End If
            con.Close()
        
        End If
    End Sub

Please suggest how can i populate my datagrid in second form by using the Data populated in Datagrid of Firrt Form

Dani AI

Generated

Short practical note — two reliable patterns that avoid fragile copy/paste of rows:

  1. Pass the already-populated data (preferably a DataTable/BindingSource) to the delete form and bind the second grid to that source; or
  2. Pass only the selected filter keys (IDs or values) and let the second form populate its own combos and grid, then restore selection by value.

’s idea of sharing the same dataset is fine; the safer, more maintainable approach is to either pass a copy of the DataTable (so the two forms don’t step on each other) or pass the selected keys and set selections by ValueMember on the second form.

Example sender (first form) — pass a copy of the grid source and the selected values:

Dim dlg As New FrmDeleteClassWiseSmsSetting()
Dim dt As DataTable = TryCast(DGVStudRecord.DataSource, DataTable)
If dt IsNot Nothing Then dlg.GridSnapshot = dt.Copy()
dlg.ClassKey = CBClass.SelectedValue
dlg.MediumKey = CBMedium.SelectedValue
dlg.ShowDialog()

Example receiver (second form) — bind the grid and then restore combo selection by value:

Public Property GridSnapshot As DataTable
Public Property ClassKey As Object
Public Property MediumKey As Object

Private Sub FrmDeleteClassWiseSmsSetting_Load(...) Handles MyBase.Load
    If GridSnapshot IsNot Nothing Then DGVStudRecord.DataSource = GridSnapshot

    ' Populate CBClass/CBMedium here (set DataSource, DisplayMember, ValueMember)
    ' After binding, restore selection by SelectedValue:
    If ClassKey IsNot Nothing Then CBClass.SelectedValue = ClassKey
    If MediumKey IsNot Nothing Then CBMedium.SelectedValue = MediumKey
End Sub

Troubleshooting notes:

  • Do not rely on SelectedText — it is not the right property for selecting an item; use SelectedValue or SelectedIndex.
  • If the combo is filled later (or order differs), set SelectedValue after the combo’s DataSource is set (Form.Shown or immediately after the Fill call).
  • If you want changes in one form to appear in the other, bind them to the same DataTable instance; for an independent snapshot use dt.Copy().

This keeps logic clear and avoids manually looping to add rows or fragile index-based selection.

Recommended Answers

All 5 Replies

When u click on delete button
bind the second grid also with same dataset as firstform

If dr.HasRows Then
                While dr.Read
                  
                    DGVStudRecord.Rows.Add(dr(0), dr(1), dr(2), dr(3), dr(4), dr(5))
                    form2.grid.rows.add(dr(0), dr(1), dr(2), dr(3), dr(4), dr(5))

                End While
            End If

Thanks mate its working accrding to my requirement.I want to do the same for my Comboboxes.
There are two Comboboxes.I want that the as soon as my second form is shown my comboboxes should have the same selected values that are selected on Form one.Comboboxes on boththe forms contains same Elements as they are populated from Database with the same Query at the Load Event of their respective forms.I have tried to use Object as well as the method u showed to fill second Datagrid.Its still not working.
FrmDeleteClassWiseSmsSetting-second Form
CBClass(on left of =)-Combo on second Form
CBClass(on Right of =)-Combo on First Form

FrmDeleteClassWiseSmsSetting.CBClass.SelectedText = CBClass.SelectedText

Thanks mate im Done with my Issues.This is what im doing at the Load of second form to get my combo selected depending on the selection of Combo on First form.

CBClass.SelectedIndex = FrmSMSMobileNumberSetting.CBClass.SelectedIndex
   CBMedium.SelectedIndex = FrmSMSMobileNumberSetting.CBMedium.SelectedIndex

hi Vikram,
Mine is a windows application.

Thanks Mate Vikram.
But im done with my issue.
Thanks for ur concern.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.