Export (0) Print
Expand All

DataGrid.GetCellBounds Method

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

Overload List

Gets the Rectangle of the cell specified by DataGridCell.

[Visual Basic] Overloads Public Function GetCellBounds(DataGridCell) As Rectangle
[C#] public Rectangle GetCellBounds(DataGridCell);
[C++] public: Rectangle GetCellBounds(DataGridCell);
[JScript] public function GetCellBounds(DataGridCell) : Rectangle;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function GetCellBounds(Integer, Integer) As Rectangle
[C#] public Rectangle GetCellBounds(int, int);
[C++] public: Rectangle GetCellBounds(int, int);
[JScript] public function GetCellBounds(int, int) : Rectangle;

Example

[Visual Basic, C#, C++] The following example uses the GetCellBounds method to return a Rectangle of a specified cell.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetCellBounds. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Protected Sub dataGrid1_MouseDown _
(sender As Object, e As MouseEventArgs)
   ' Use the HitTest method to get a HitTestInfo object.
   Dim hi As System.Windows.Forms.DataGrid.HitTestInfo
   Dim grid As DataGrid = CType(sender, DataGrid)
   hi = grid.HitTest(e.X, e.Y)
   ' Test if the clicked area was a cell.
   If hi.Type = DataGrid.HitTestType.Cell Then
   ' If it's a cell, get the GridTable and CurrencyManager of the
   ' clicked table.         
   Dim dgt As DataGridTableStyle = dataGrid1.TableStyles(0)
   Dim myCurrencyManager As CurrencyManager = _
   CType(Me.BindingContext _
   (myDataSet.Tables(dataGrid1.DataMember)), CurrencyManager)
   ' Get the Rectangle of the clicked cell.
   Dim cellRect As Rectangle
   cellRect = grid.GetCellBounds(hi.Row, hi.Column)
   ' Get the clicked DataGridTextBoxColumn.
   Dim gridCol As DataGridTextBoxColumn = _
   CType(dgt.GridColumnStyles(hi.Column), DataGridTextBoxColumn)
      ' Insert code to dit the value.
   End If
End Sub 

[C#] 
protected 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.
      
   }
}


[C++] 
protected:
void dataGrid1_MouseDown(Object* sender, MouseEventArgs* e){
   // Use the HitTest method to get a HitTestInfo object.
   System::Windows::Forms::DataGrid::HitTestInfo* hi;   
   DataGrid* grid = dynamic_cast<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->Item[0];     
      CurrencyManager* myCurrencyManager = 
        dynamic_cast<CurrencyManager*>(BindingContext->Item[myDataSet->Tables->Item[dataGrid1->DataMember]]);
      // Get the Rectangle of the clicked cell.
      Rectangle cellRect;
      cellRect=grid->GetCellBounds(hi->Row, hi->Column);
      // Get the clicked DataGridTextBoxColumn.
      DataGridTextBoxColumn* gridCol =
        dynamic_cast<DataGridTextBoxColumn*> (dgt->GridColumnStyles->Item[hi->Column]);
      // Insert code to edit the value.
      
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

DataGrid Class | DataGrid Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft