DataGrid.GetCellBounds Method

Definition

Gets the Rectangle that specifies the four corners of a cell.

Overloads

GetCellBounds(Int32, Int32)

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

GetCellBounds(DataGridCell)

Gets the Rectangle of the cell specified by DataGridCell.

GetCellBounds(Int32, Int32)

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

C#
public System.Drawing.Rectangle GetCellBounds(int row, int col);

Parameters

row
Int32

The number of the cell's row.

col
Int32

The number of the cell's column.

Returns

A Rectangle that defines the current cell's corners.

Examples

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

C#
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 = 
      (CurrencyManager)this.BindingContext
      [myDataSet.Tables[dataGrid1.DataMember]];
      // 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.
   }
}

Remarks

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

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0

GetCellBounds(DataGridCell)

Gets the Rectangle of the cell specified by DataGridCell.

C#
public System.Drawing.Rectangle GetCellBounds(System.Windows.Forms.DataGridCell dgc);

Parameters

dgc
DataGridCell

The DataGridCell to look up.

Returns

A Rectangle that defines the current cell's corners.

Examples

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

C#
protected DataGridCell dgc;

protected void GetRect(){
    Rectangle rect;
    dgc.ColumnNumber = 0;
    dgc.RowNumber = 0;
    rect = dataGrid1.GetCellBounds(dgc);
    Console.WriteLine(rect.ToString());
 }

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0