A.Smallwood A.Smallwood - 10 months ago 69
C# Question

DataGridView Value to a textbox on an already Active Form

Form1 has a text box with a button that brings up Form2 that is a filters screen for Form3 which has the DataGridView. When I doubleclick on a DataGridView Row, I need it to pass a value to the TextBox on Form1 (which will stay open and Active) I can get the correct value to display in a MessageBox when I double click the row but I can't seem to pass the value the Form1.


private void btnDepartmentSearch_Click(object sender, EventArgs e)
Employee_Department_Search_Filters EDSF = new Employee_Department_Search_Filters();
EDSF.Owner = this;


private void search()
Employee_Department_Search_Results EDSR = new Employee_Department_Search_Results();
EDSR.Owner = this.Owner;

string departmentContains = tbDepartment.Text;

adapt = new SqlDataAdapter("select uid_department, str_code AS [Department Code], str_name AS [Department Name] from tbl_department where str_name like '%" + departmentContains + "%'", conString);
ds = new System.Data.DataSet();
adapt.Fill(ds, "Department");
EDSR.dgvDepartmentSearch.DataSource = ds.Tables[0];

EDSR.dgvDepartmentSearch.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
EDSR.dgvDepartmentSearch.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
EDSR.dgvDepartmentSearch.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

EDSR.dgvDepartmentSearch.Columns[0].Visible = false;



Form 3

public void rowClick()
Employee_Maintenance EM = new Employee_Maintenance();

SqlConnection conn = new SqlConnection(myconnection);
SqlCommand comm = new SqlCommand("select str_code from tbl_department where uid_department = " + dgvDepartmentSearch.SelectedCells[0].Value, conn);
SqlDataReader read = null;
read = comm.ExecuteReader();

departmentCode = read["str_code"].ToString();

EM.txtDepartment.Text = departmentCode;


Answer Source

Try this:

public void rowClick()
    // ...

    ((Employee_Maintenance)this.Owner).txtDepartment.Text = departmentCode;

    // ...