DataGrid.GetCellBounds Method (Int32, Int32)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets the Rectangle of the cell specified by row and column number.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public Rectangle GetCellBounds(
	int row,
	int col


Type: System.Int32

The number of the cell's row.

Type: System.Int32

The number of the cell's column.

Return Value

Type: System.Drawing.Rectangle

A Rectangle that defines the current cell's corners.

To retrieve the cell bounds for the currently selected cell, use GetCurrentCellBounds.

The following code example uses the GetCellBounds method to return a Rectangle of a specified cell.

private void dataGrid1_MouseDown(object sender, MouseEventArgs e){
   // Use the HitTest method to get a HitTestInfo object.
   System.Windows.Forms.DataGrid.HitTestInfo hi;   
   DataGrid grid = (DataGrid) sender;
   hi=grid.HitTest(e.X, e.Y);
   // Test if the clicked area was a cell.
   if(hi.Type==DataGrid.HitTestType.Cell ) {
      // If it's a cell, get the GridTable and CurrencyManager of the
      // clicked table.         
      DataGridTableStyle dgt = dataGrid1.TableStyles[0];     
      CurrencyManager myCurrencyManager = 
      // Get the Rectangle of the clicked cell.
      Rectangle cellRect;
      cellRect=grid.GetCellBounds(hi.Row, hi.Column);
      // Get the clicked DataGridTextBoxColumn.
      DataGridTextBoxColumn gridCol =
      (DataGridTextBoxColumn) dgt.GridColumnStyles[hi.Column];
      // Insert code to edit the value.


.NET Framework
Available since 1.1
Return to top