DataGrid.Item[] Property

Definition

Gets or sets the value of a specified cell.

Overloads

Item[DataGridCell]

Gets or sets the value of a specified DataGridCell.

Item[Int32, Int32]

Gets or sets the value of the cell at the specified the row and column.

Item[DataGridCell]

Gets or sets the value of a specified DataGridCell.

C#
public object this[System.Windows.Forms.DataGridCell cell] { get; set; }

Parameters

cell
DataGridCell

A DataGridCell that represents a cell in the grid.

Property Value

The value, typed as Object, of the cell.

Examples

The following code example sets and gets the value of a cell by declaring a DataGridCell variable, setting its RowNumber and ColumnNumber values, then first changing, then returning, the value of the given cell.

C#
private void SetCellValue(DataGrid myGrid){
   DataGridCell myCell = new DataGridCell();
   // Use an arbitrary cell.
   myCell.RowNumber = 1;
   myCell.ColumnNumber = 1;
   // Change the cell's value using the CurrentCell.
   myGrid[myCell]="New Value";
}
 
private void GetCellValue(DataGrid myGrid){
   DataGridCell myCell = new DataGridCell();
   // Use and arbitrary cell.
   myCell.RowNumber = 1;
   myCell.ColumnNumber = 1;
   Console.WriteLine(myGrid[myCell]);
}

Remarks

Setting this property changes the position of the DataView to the specified row.

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

Item[Int32, Int32]

Gets or sets the value of the cell at the specified the row and column.

C#
public object this[int rowIndex, int columnIndex] { get; set; }

Parameters

rowIndex
Int32

The zero-based index of the row containing the value.

columnIndex
Int32

The zero-based index of the column containing the value.

Property Value

The value, typed as Object, of the cell.

Exceptions

While getting or setting, the rowIndex is out of range.

While getting or setting, the columnIndex is out of range.

Examples

The following code example prints the value contained by the cell at the specified row and index.

C#
private void PrintCellValues(DataGrid myGrid){
    int iRow;
    int iCol;
    DataTable myTable;
    // Assumes the DataGrid is bound to a DataTable.
    myTable = (DataTable) dataGrid1.DataSource;
    for(iRow = 0;iRow < myTable.Rows.Count ;iRow++) {
       for(iCol = 0;iCol < myTable.Columns.Count ;iCol++) {
          Console.WriteLine(myGrid[iRow, iCol]);
       }
    }
 }

Remarks

Setting this property changes the position of the DataView to the specified row.

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