2012-02-22 35 views
0
 private void item_grid_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (e.ColumnIndex==taxone_col_index || e.ColumnIndex==taxtwo_col_index) 
     { 


     } 

    } 

    private void item_grid_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == (char)Keys.Return) 
     { 
      e.Handled = true; 

      item_grid.CellClick; // i did this but its not working   } 
    } 

我想從按鍵事件中執行單元格單擊事件。怎麼做?如何從datagriview keypress事件執行datagridview cellclick事件?

回答

2

這聽起來像你想要的是知道鍵被按下的單元格的行和列索引。然後,您將能夠查找單元格的值。

要做到這一點,只需使用DataGridViewCurrentCell財產。

試圖人爲地創建一個CellClick只是要求麻煩。

有一點要注意的是,你可能需要處理EditingControlShowing事件和附加KeyPress處理底層編輯控制,因爲輸入到DataGridView細胞不提升電網水平KeyPress事件。


如果你真的想創建一個CellClick情況下,您將需要繼承DataGridView控制,並創建自己的RaiseCellClick()方法,然後調用保護OnCellClick()方法:

public void RaiseCellClick(int row, int column) 
{ 
    DataGridViewCellEventArgs e = new DataGridViewCellEventArgs(row, column); 
    base.OnCellClick(e); 
} 

但即使這並不是特別有用,因爲DataGridViewCellEventArgs需要在其構造函數中使用行列索引。

+0

你的代碼給了我主意,我解決了我的問題..謝謝 – 2012-02-22 16:19:05