2017-05-07 44 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