现在又两个窗体。form1,form2
form1上有一个datagridview。当我双击行的时候弹出模态的form2窗体。
并将datagridview当前行的值传递到form2窗体中的textbox中。我的代码有错误,提示未将对象实例化        private void dgvRecord_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                frmModMachine newfrmMachine = new frmModMachine();
                ((TextBox)newfrmMachine.Controls["txtNO"]).Text = dgvRecord.Rows[e.RowIndex].Cells[2].Value.ToString();
                newfrmMachine.ShowDialog();
            }
        }

解决方案 »

  1.   

    ((TextBox)newfrmMachine.Controls["txtNO"]).Text = dgvRecord.Rows[e.RowIndex].Cells[2].Value.ToString();改为:TextBox txt = newfrmMachine.Controls["txtNO"] as TextBox;
    if(txt != null)
    {
        txt=dgvRecord.Rows[e.RowIndex].Cells[2].Value.ToString();
    }
      

  2.   

     ((TextBox)newfrmMachine.Controls["txtNO"])
    这个控件是私有的,不能在form1传
    你可以在这么写form2:
     public Form2(string value)
            {
                InitializeComponent();
                txtNO.TEXT=value;
            }
    from1:
     private void dgvRecord_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex != -1)
                {
                    frmModMachine newfrmMachine = new frmModMachine(dgvRecord.Rows[e.RowIndex].Cells[2].Value.ToString());
                    newfrmMachine.ShowDialog();
                }
            }
      

  3.   


    form2: 
    public Form2(string value) 
            { 
                InitializeComponent(); 
                txtNO.TEXT=value; 
            }
    from1: 
    private void dgvRecord_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
            { 
                if (e.RowIndex != -1) 
                { 
                    frmModMachine newfrmMachine = new frmModMachine(dgvRecord.Rows[e.RowIndex].Cells[2].Value.ToString()); 
                    newfrmMachine.ShowDialog(); 
                } 
            } 
      

  4.   


    1,你的那个TextBox的名字确实是 txtNO ?
    2,dgvRecord有没有第三列?要是上述都都满足。那么代码是对的