Pass data from Windows Form DataGridView to Child MDI Form using C#

Bhavesh23
 
on Jul 20, 2022 07:04 AM
453 Views

How to pass required record from form1 to form2 in textboxes from DataGridView.

In form1 i have 1 DataGridView now i want to pass required record like name, code, address, pin etc in TextBoxes on form2 using window c# application.

I have searched on Google but still not understand how to do this.

When user double click on row or enter on row the form1 should close and record should pass on form2 in textboxes.

 i have used mdi page to maintain all pages,

frmNewInvoice fr = new frmNewInvoice();
int row = gvCustomer.CurrentRow.Index;
fr.txtpartycode.Text = Convert.ToString(gvCustomer[0, row].Value);
fr.txtpartyname.Text = Convert.ToString(gvCustomer[1, row].Value);
fr.txtpartyaddress.Text = Convert.ToString(gvCustomer[2, row].Value);
fr.Show();
this.Hide();

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Jul 21, 2022 03:25 AM
on Jul 21, 2022 04:24 AM

Hi Bhavesh23,

You need to check the ActiveMdiChild. If it is not null then close it first before opening the new one.

Please refer below sample.

Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.

Namespace

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

Form1

public Form1()
{
    InitializeComponent();
    this.BindDataGridView();
    this.IsMdiContainer = true;
}

private void BindDataGridView()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] {
                        new DataColumn("Id"),
                        new DataColumn("Name"),
                        new DataColumn("Country") });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    dgvCustomers.DataSource = dt;
}

public static DataGridViewRow SelectedRow { get; set; }
private void OnCellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        if (ActiveMdiChild != null)
        {
            ActiveMdiChild.Close();
        }
        SelectedRow = dgvCustomers.Rows[e.RowIndex];
        Form2 frm = new Form2();
        frm.MdiParent = this;
        frm.Show();
    }
}

Form2

public Form2()
{
    InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
    txtId.Text = Form1.SelectedRow.Cells[0].Value.ToString();
    txtName.Text = Form1.SelectedRow.Cells[1].Value.ToString();
    txtCountry.Text = Form1.SelectedRow.Cells[2].Value.ToString();
}

VB.Net

Form1

Public Sub New()
    InitializeComponent()
    Me.BindDataGridView()
    Me.IsMdiContainer = True
End Sub

Private Sub BindDataGridView()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    dgvCustomers.DataSource = dt
End Sub

Public Shared Property SelectedRow As DataGridViewRow

Private Sub OnCellMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles dgvCustomers.CellMouseClick
    If e.RowIndex >= 0 Then

        If ActiveMdiChild IsNot Nothing Then
            ActiveMdiChild.Close()
        End If

        SelectedRow = dgvCustomers.Rows(e.RowIndex)
        Dim frm As Form2 = New Form2()
        frm.MdiParent = Me
        frm.Show()
    End If
End Sub

Form2

Public Sub New()
    InitializeComponent()
End Sub

Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    txtId.Text = Form1.SelectedRow.Cells(0).Value.ToString()
    txtName.Text = Form1.SelectedRow.Cells(1).Value.ToString()
    txtCountry.Text = Form1.SelectedRow.Cells(2).Value.ToString()
End Sub

Screenshot