2017-05-07 45 views
0

是否有人知道如何製作,雙擊DataGridView中的單元格時會出現一個包含更多信息的消息框。因此,例如我想我的DataGridView只顯示名稱和姓氏,但是當你雙擊它的消息框出現更多的信息,如年齡,高度...DoubleClick在DataGridView上獲取更多信息

感謝您的幫助!

+0

編碼DGV的「CellDoubleClick」事件!它具有單擊單元格的Row和ColumnIndices。 – TaW

回答

0

首先,你將需要訂閱「CellDoubleClick」事件,像這樣:

yourDataGridView.CellDoubleClick += yourDataGridView_CellDoubleClick(); 

這將導致你的程序啓動監聽雙擊。在同一個類中,您必須定義雙擊DataGridView時所需的行爲。 DataGridViewCellEventArgs參數具有當前行(e.RowIndex)和當前列(e.ColumnIndex)的值。下面是使用我的一個DataGridView的示例:

private void dgvContacts_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { 
     //Make sure that the user double clicked a cell in the main body of the grid. 
     if (e.RowIndex >= 0) { 
      //Get the current row item. 
      Contact currentContact = (Contact)dgvContacts.Rows[e.RowIndex].DataBoundItem; 
      //Do whatever you want with the data in that row. 
      string name = currentContact.Name; 
      string phoneNum = currentContact.Phone; 
      string email = currentContact.Email; 
      MessageBox.Show("Name: " + name + Environment.NewLine + 
       "Phone number: " + phoneNum + Environment.NewLine + 
       "Email: " + email); 
     }//if 
    }//dgvContacts_CellDoubleClick