Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get clicked cell column in DevExpress XtraGrid

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

like image 701
Nate Avatar asked Sep 14 '12 10:09

Nate


1 Answers

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

like image 173
DmitryG Avatar answered Oct 18 '22 09:10

DmitryG