Met*_*d89 23 c# events datagridview double-click
我的C#应用程序中有一个datagridview,用户应该只能点击完整的行.所以我将SelectionMode设置为FullRowSelect.
但是现在我想要一个在用户双击一行时触发的事件.我想在MessageBox中有行号.
我尝试了以下方法:
this.roomDataGridView.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.roomDataGridView_CellCont? ?entDoubleClick);
private void roomDataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(e.RowIndex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
不幸的是没有任何反应.我究竟做错了什么?
cih*_*a87 20
在CellContentDoubleClick事件中,仅在双击单元格内容时才会触发.我用过这个并且有效:
private void dgvUserList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(e.RowIndex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
不要手动编辑Visual Studio中的.designer文件,这通常会导致头痛.而是在DataGridRow的属性部分中指定它,它应该包含在DataGrid元素中.或者,如果您只是希望VS为您执行此操作,请在属性页面 - >事件(小闪电图标)中找到双击事件,然后双击要为该事件输入函数名称的文本区域.
这个链接应该有帮助
http://msdn.microsoft.com/en-us/library/6w2tb12s(v=vs.90).aspx
以northwind数据库员工表为例,获取datagridview中行的索引号:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'nORTHWNDDataSet.Employees' table. You can move, or remove it, as needed.
this.employeesTableAdapter.Fill(this.nORTHWNDDataSet.Employees);
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
var dataIndexNo = dataGridView1.Rows[e.RowIndex].Index.ToString();
string cellValue = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
MessageBox.Show("The row index = " + dataIndexNo.ToString() + " and the row data in second column is: "
+ cellValue.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果将显示记录的索引号以及 datagridview 中第二个表列的内容:
| 归档时间: |
|
| 查看次数: |
90651 次 |
| 最近记录: |