FlexGrid for WinForms | ComponentOne
Column / Basic Operations
In This Topic
    Basic Operations
    In This Topic

    This topic discusses various basic operations which can be performed on a column.

    Set Column Count

    When grid is bound to a data source, the number of columns is determined by the number of fields available in the data source. However, in the case of unbound mode, you can set any arbitrary value in the Count property of the ColumnCollection class to set the number of columns to be displayed in the grid.

    Use the code below to set the number of columns to be added to WinForms FlexGrid in unbound mode.

     // Set column count
     c1FlexGrid1.Cols.Count = 4;
    
    ' Set column count
    c1FlexGrid1.Cols.Count = 4        
    

    Add Column

    FlexGrid provides two ways to add a new column using the ColumnCollection class. You can either use Add method or increment the value of Count property provided by the class to add new columns. Also, note that if a new column is added to a bound grid using these methods, it is added as an unbound column only.

    Following are the two approaches to add column to a WinForms FlexGrid.

        // Approach1: 
        // Use Add method of the column collection
        C1.Win.C1FlexGrid.Column c;
        c = c1FlexGrid1.Cols.Add();
        c.Caption = "New Column";
    
        //Approach2:
        // Change Count property of the column collection
        c1FlexGrid1.Cols.Count += 1;
        c1FlexGrid1.Cols[c1FlexGrid1.Cols.Count - 1].Caption = "New Column";
    
        ' Approach1: 
        ' Use Add method of the column collection
        Dim c As C1.Win.C1FlexGrid.Column
        c = c1FlexGrid1.Cols.Add()
        c.Caption = "New Column"
    
        'Approach2:
        ' Change Count property of the column collection
        c1FlexGrid1.Cols.Count += 1
        c1FlexGrid1.Cols(c1FlexGrid1.Cols.Count - 1).Caption = "New Column"         
    

    Delete Column

    To delete a particular column from the grid, you can use Remove method of the ColumnCollection class and specify the column you want to delete as its parameter. In the unbound grid, you can also reduce the number of columns by changing the value of Count property. This removes columns from the end of the column collection. The ColumnCollection class also provides RemoveRange method which allows you to remove a range of columns.

    Below code snippet shows all the three approaches to delete column from the WinForms FlexGrid.

       // Approach1:
       // Delete 2nd column using the Remove method of column collection      
       c1FlexGrid1.Cols.Remove(2);
    
       // Approach2:
       // Delete the last column by changing the Count property
       c1FlexGrid1.Cols.Count -= 1;
    
       // Approach3:
       // Delete four columns starting from 2nd by using the RemoveRange method 
       c1FlexGrid1.Cols.RemoveRange(2, 4);                                      
    
        ' Approach1:
        ' Delete 2nd column using the Remove method of column collection      
        c1FlexGrid1.Cols.Remove(2)
    
        ' Approach2:
        ' Delete the last column by changing the Count property
        c1FlexGrid1.Cols.Count -= 1
    
        ' Approach3:
        ' Delete four columns starting from 2nd by using the RemoveRange method 
        c1FlexGrid1.Cols.RemoveRange(2, 4)       
    

    Insert Column

    To insert a column in FlexGrid at a specific location, you can use Insert method of the ColumnCollection class which lets you specify the position where columns are to be inserted. You can also insert multiple columns in the grid by using the InsertRange method. These methods add the unbound columns only, even if the grid is a bound grid.

    Use any of the following approaches to insert a column in the WinForms FlexGrid at the specified location.

        C1.Win.C1FlexGrid.Column c;
    
       // Approach1:
       // Insert a column at the 2nd position using the Insert method
       c = c1FlexGrid1.Cols.Insert(2);
       c.Caption = "Inserted Column";
    
       // Approach2:
       // Insert three columns at 2nd position using the InsertRange method
       // c1FlexGrid1.Cols.InsertRange(2, 3);                            
    
     Dim c As C1.Win.C1FlexGrid.Column
    
    'Approach1:
    'Insert a column at the 2nd position using the Insert method
     c = c1FlexGrid1.Cols.Insert(2)
     c.Caption = "Inserted Column"
                                            
    'Approach2:
    'Insert three columns at 2nd position using the InsertRange method
    'c1FlexGrid1.Cols.InsertRange(2, 3)
                                            
    

    Set Data Type

    In case of a bound FlexGrid, data type of each bound column is automatically picked from the data source depending on the data. However, in the case of unbound mode, you can set the data type of columns by specifying the DataType property of Column class. You can also set the Data Type property at design time by using the Column Tasks menu. For more information on tasks menus, see Tasks Menus. Note that when a particular data type is set to a FlexGrid column, its cells accept the user input of keys accepted by the data type only. For instance, you can not input alphabets in a Numeric type cell.

    Set the data type of an unbound column in WinForms FlexGrid as shown in the code below.

     // Set data type of first column
     c1FlexGrid1.Cols[1].DataType = typeof(int);                     
    
    ' Set data type of first column
    c1FlexGrid1.Cols(1).DataType = GetType(Integer)
                                    
    

    Set Fixed Column

    Fixed columns refer to the columns with non-editable cells which are always visible in the left hand side of the grid even if user scrolls the grid horizontally. In FlexGrid, fixed columns can be set using Fixed property of the ColumnCollection class. This property accepts an integer value that specifies the number of columns to be fixed.

    Use the code below to set one column as fixed in the WinForms FlexGrid.

    // Set one column as fixed
    c1FlexGrid1.Cols.Fixed = 1;                            
    
    ' Set one column as fixed
    c1FlexGrid1.Cols.Fixed = 1          
    

    Set Frozen Column

    Frozen columns, similar to fixed columns, are non-scrollable but can be edited by the user. In FlexGrid, frozen columns can be set by using Frozen property provided by the ColumnCollection class.

    To set frozen columns in the WinForms FlexGrid, use the code below.

    // Set first four columns as frozen 
    c1FlexGrid1.Cols.Frozen = 4;
    
    ' Set first four columns as frozen 
    c1FlexGrid1.Cols.Frozen = 4       
    
    See Also