Here I am going to show how to display grid record when user clicks on any row in the form grid. Below I have attached an screenshot of form layout in which we will display data of selected grid record.
I have created a form 'Employee Info'
Show GridView record from one Form to another Form

now, when we click on Browse button (that is in circle), then it will open another Form '
Employee List' like this :
Show GridView record from one Form to another Form


But main task is this, when user click on any row of the GridView then its all data are populated on Employee Info Form and result is:

Show GridView record from one Form to another Form

Going on Solution

Step 1:  Click on browse button on 'Employee Info' form and write the below code:

private void btnBrowse_Click(object sender, EventArgs e)
{
         Form2 frm = new Form2(this);
         frm.ShowDialog();
 }


Step 2: Now write given code on Employee List Form:

public partial class Form2 : Form
{
Form1 empForm;

public Form2(Form1 EmpForm)
{
InitializeComponent();
this.empForm = EmpForm;
}

private void GridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
empForm.FillEmployeeData(dgEmployee.Rows[e.RowIndex]);
this.Close();
}
}
}


where Employee Info form has the definition of FillEmployeeData() method :

public void FillEmployeeData(DataGridViewRow row)
{
ClearFields();
btnSave.Enabled = false;
btnUpdate.Enabled = true;
btnDelete.Enabled = true;
Employee data = sm.EmployeeRepository.Find(int.Parse(row.Cells["ID"].Value.ToString()));
txt_id.Text = data.ID.ToString();
txt_name.Text = data.Name;
txt_father.Text = data.FatherName;
dtpDOB.Value = data.DOB ?? DateTime.Now;
cmbCity.Text = data.City;
txt_address.Text = data.Address;
}

I hope you are understand this code. If any advice or problem please comment.

  Modified On Nov-28-2017 08:21:45 PM

Leave Comment