True DBGrid for WinForms | ComponentOne
Cells / Basic Operations
In This Topic
    Basic Operations
    In This Topic

    Set Value in Cell

    True DBGrid lets you set a value in the True DBGrid cell.

    C#
    Copy Code
    //set value in a cell
    c1TruedbGrid1[0, 0] = "Single Cell Data Set";
    

    Set Values in Cell Range

    The Grid control also lets you set values in cell ranges in the grid.

    The following code shows how to set values in a cell range in True DBGrid.

    C#
    Copy Code
    //set value in a range {Custom method is used; declared below} there is no public cell range in TDbGrid
    c1TruedbGrid1.SetRangeData(1, 4, 0, 2, "Data");
    

    The SetRangeData method is taken from the following class:

    C#
    Copy Code
    public static class Extensions
    {
    //extension method to set data in a cell range
            public static void SetRangeData(this C1TrueDBGrid grid, int rowIdx,int rowCount,int colIdx,int colCount,object? data)
       {
                if (rowCount == 0 || colCount == 0) return;
                //check if index is valid
                if (grid.Columns.Count <= colIdx || grid.RowCount <= rowIdx) return;
                for (int row = rowIdx; row < rowIdx + rowCount; row++)
             {
                    for (int col = colIdx; col < colIdx + colCount; col++)
                   {
                        //set data
                        grid[row, col] = data;
                   }
             }
      }
    }
    

    Clear Value from Cell (Range)

    The True DBGrid control lets you clear values from cell ranges in the grid.

    The following code shows how to clear values from cells in True DBGrid.

    C#
    Copy Code
    //clear a range
    c1TruedbGrid1.SetRangeData(1, 2, 0, 2, null);
    

    The SetRangeData method is taken from the following class:

    C#
    Copy Code
    public static class Extensions
     {
     //extension method to set data in a cell range
             public static void SetRangeData(this C1TrueDBGrid grid, int rowIdx,int rowCount,int colIdx,int colCount,object? data)
        {
                 if (rowCount == 0 || colCount == 0) return;
                 //check if index is valid
                 if (grid.Columns.Count <= colIdx || grid.RowCount <= rowIdx) return;
                 for (int row = rowIdx; row < rowIdx + rowCount; row++)
              {
                     for (int col = colIdx; col < colIdx + colCount; col++)
                    {
                         //set data
                         grid[row, col] = data;
                    }
              }
       }
     }
    

    Set Image in Cell

    To set image in a cell, you can use the FetchStyle property to get or set a value indicating whether the FetchCellStyle event will be raised for a column.

    Use the code below to set image in a True DBGrid cell.

    C#
    Copy Code
    //show images in the third column
    c1TruedbGrid1.Splits[0].DisplayColumns[2].FetchStyle = true;
    c1TruedbGrid1.FetchCellStyle += C1TruedbGrid1_FetchCellStyle;
    

    The FetchCellStyle event is set as given in the code snippet below:

    C#
    Copy Code
    private void C1TruedbGrid1_FetchCellStyle(object sender, FetchCellStyleEventArgs e)
            {
                if (e.Col ==2) {
                    //set the image
                    e.CellStyle.ForegroundImage = Resource1.temp2;
                }
            }
    

    Enable Tooltip in Cell

    To enable tooltip in cell, use the CellTipEnum enumeration, which provides the following values:

    CellTipEnum values Description
    Anchored Cell tips will be displayed in the bounding rectangle of the cell.
    Floating Cell tips will be displayed under the mouse cursor.
    NoCellTips No cell tips will be displayed.

    Below code shows how to display a tooltip on a True DBGrid cell.

    C#
    Copy Code
    //enable Cell ToolTips
    c1TruedbGrid1.CellTips = CellTipEnum.Floating;