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