2014-07-17 26 views
1

我被困在編程問題上。當我右鍵單擊一個DataGridView對象的單元格時,會顯示一個上下文菜單。從該上下文菜單中,用戶可以點擊一個選項。該選項將顯示一個表單。用戶完成後,該表單的結果將填充到所選單元格中。無法從子窗體獲取數據到選定的父DataGridView單元格

問題:如何從子窗體獲得結果到選定的單元格中?父表單上有多個使用相同功能的DataGridView對象。

代碼:

private void commandOperation(Object sender, EventArgs e) 
    { 
     if (sender == dec2HexToolStripMenuItem) 
     { 
      frmNumFormatConv form = new frmNumFormatConv(); 
      if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       // Get data from the form into the selected cell!!! 
      } 
     } 
     else 
     { 
      throw new Exception("Operational object unknown"); 
     } 
    } 

    private void cellMouseDown(Object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if (sender == dgvCircuit1TestSetup || sender == dgvCircuit2TestSetup) 
     { 
      if (e.Button == MouseButtons.Right) 
      { 
       ((DataGridView)sender).CurrentCell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex]; 
       itsGridContextMenu.Show((DataGridView)sender, new Point(e.X, e.Y)); 
      } 
     } 
    } 
+0

有你接過來一看,以http://stackoverflow.com/questions/1516252/如何以編程方式設置單元格值datagridview – tschmit007

回答

0

一個非常簡單的方法是把在彈出形式的公共財產,將返回所需的值(說RtnValue)。然後

這是彈出的形式的例子:

public string RtnValue 
    { 
     set { textBox1.Text = value; } 
    } 

這是當前的代碼:

frmNumFormatConv form = new frmNumFormatConv(); 
     if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      dgvalue = form.RtnValue; 
      // Get data from the form into the selected cell!!! 
     } 
+0

但是,如何獲取所選DataGridView對象的單元格中返回的值?在CellMouseDown方法中,我知道正在處理哪個DataGridView對象。當我到達CommandOperation方法時,我無法綁定來自哪個DataGridView。這是我一直試圖弄清楚的問題。我可能必須將活動的DataGridView對象保存爲全局對象,並在完成後清除對象。 – user3826668

+0

您確定該信息不在EventArgs e對象中嗎? – bowlturner

+0

其實,我希望信息是在EventArgs e對象中。我只是不知道如何從中提取出來。將它作爲DataGridViewCellMouseEventArgs投擲EventArgs? – user3826668

相關問題