DataGridView值已经在活动表单上的文本框中

时间:2016-10-05 11:44:51

标签: c#

Form1有一个文本框,其中包含一个按钮,该按钮可以显示Form2,它是Form3的过滤器屏幕,具有DataGridView。当我双击DataGridView行时,我需要它将值传递给Form1上的TextBox(它将保持打开和活动状态)当我双击行时我可以获得正确的值以显示在MessageBox中但我可以&#39 ; t似乎传递了Form1的值。

Form1中

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

窗体2

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;

        this.Close();

        EDSR.ShowDialog(this.Owner);
    }

表格3

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

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

        while(read.Read())
        {
            departmentCode = read["str_code"].ToString();
        }

        EM.txtDepartment.Text = departmentCode;

        conn.Close();
        this.Close();
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

public void rowClick()
{
    // ...

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

    // ...
}