C# 如何在 DevExpress XtraGrid 中获得点击的单元格列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12422680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get clicked cell column in DevExpress XtraGrid
提问by Nate
I can't get column name of clicked cell in GridControl of XtraGrid. How can I do that? I'm handling GridView.Clickevent.
我无法在 XtraGrid 的 GridControl 中获取单击单元格的列名。我怎样才能做到这一点?我正在处理GridView.Click事件。
采纳答案by DmitryG
Within the click event you can resolve the clicked cell as follows:
在单击事件中,您可以按如下方式解析单击的单元格:
void gridView_Click(object sender, EventArgs e) {
Point clickPoint = gridControl.PointToClient(Control.MousePosition);
var hitInfo = gridView.CalcHitInfo(clickPoint);
if(hitInfo.InRowCell) {
int rowHandle = hitInfo.RowHandle;
GridColumn column = hitInfo.Column;
}
}
However, I suggest you handle the GridView.MouseDown event as follows (because the GridView.Click event does not occur if clicking a grid cell activates a column editor):
但是,我建议您按如下方式处理 GridView.MouseDown 事件(因为如果单击网格单元格激活列编辑器,则不会发生 GridView.Click 事件):
gridView.MouseDown += new MouseEventHandler(gridView_MouseDown);
//...
void gridView_MouseDown(object sender, MouseEventArgs e) {
var hitInfo = gridView.CalcHitInfo(e.Location);
if(hitInfo.InRowCell) {
int rowHandle = hitInfo.RowHandle;
GridColumn column = hitInfo.Column;
}
}
Related link: Hit Information Overview
相关链接:命中信息概览

