How to get clicked cell column in DevExpress XtraGrid

Nate picture Nate · Sep 14, 2012 · Viewed 26.6k times · Source

I can't get column name of clicked cell in GridControl of XtraGrid. How can I do that? I'm handling GridView.Click event.

Answer

DmitryG picture DmitryG · Sep 14, 2012

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 += 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