How to get Row and Column index of Clicked Cell in GridDataControl?
In GridDataControl, you can get the Row and Column index of a cell, using PointToCellRowColumnIndex and PointToCellRowColumnIndexOutsideCells.
PointToCellRowColumnIndex
This method enables you to get the Row and Column index of points in GridDataControl regardless of its position, by returning index values of points outside cell region also. You cannot distinguish if you have clicked on cell or points outside cells. The syntax of this method is PointToCellRowColumnIndex (Point p).
C#:
SyncGrid.MouseDoubleClick += new MouseButtonEventHandler(SyncGrid_MouseDoubleClick); void SyncGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) {Point mouseDownPos = e.GetPosition(gdata.Model.Grid); RowColumnIndex rowcol = Syncgrid.Model.Grid.PointToCellRowColumnIndex(mouseDownPos); MessageBox.Show("" + rowcol); }
You can distinguish points inside the cells and outside, using the method PointToCellRowColumnIndexOutsideCells.
PointToCellRowColumnIndexOutsideCells
This is used to get Row and Column index of points in GridDataControl, along with a way to identify points outside cells. The syntax of this method is PointToCellRowColumnIndexOutsideCells(Point p, bool allowOutsideLines) . You can easily identify whether you have clicked at any point inside the cells or outside by setting the allowOutsideLines value as “false”. This method returns negative index values for points outside the cells when set”false”.
C#:
SyncGrid.MouseDoubleClick += new MouseButtonEventHandler(SyncGrid_MouseDoubleClick); Void SyncGrid_MouseDoubleClick (object sender, MouseButtonEventArgs e) { Point mouseDownPos = e.GetPosition(SyncGrid.Model.Grid); RowColumnIndex rowcol = SyncGrid.Model.Grid.PointToCellRowColumnIndexOutsideCells(mouseDownPos, false); if (rowcol.RowIndex > SyncGrid.Model.RowCount || rowcol.RowIndex < 0) MessageBox.Show("Clicked on the empty space"); else MessageBox.Show("clicked on cell"+"\t"+rowcol); }
If you click at points inside cells, you will get the Row and Column index in the message box, otherwise, it will show as Clicked on the empty cells.