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

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

    Set Row Count

    When grid is bound to a data source, the number of rows is determined by the number of records available in the data source. However, in the case of unbound mode, you can set any integer value in Count property of the RowCollection class to set the number of rows to be displayed in the grid.

    Use the code below to set row count in the WinForms FlexGrid.

     // Set row count
     c1FlexGrid1.Rows.Count = 15;
    
    ' Set row count
    c1FlexGrid1.Rows.Count = 15                    
    

    Add Row

    FlexGrid provides various ways to add a new row at runtime. You can either use Add method of the RowCollection class or AddItem method of the C1FlexGrid class to add a new record. In the unbound mode, you can also increment the value of Count property to add new rows. Note that all these ways add rows towards the end of the grid. To insert a row at a specific location, see Insert Row. Also, note that an exception is thrown if a new row is added to a bound grid using Count property.

    Add a row to WinForms FlexGrid using any of these approaches shown in the code below.

                                      
    // Approach1:
    // Use the RowCollection.Add method to add row
     C1.Win.C1FlexGrid.Row r;
     r = c1FlexGrid1.Rows.Add();
             
    // Set data
     r[1] = "New Row 2";
    
     // Approach2: 
     // Use AddItem method to add row and set data
      c1FlexGrid1.AddItem("" + "\t" + "New Row 1");
           
    // Approach3:
    // Use the RowCollection.Count property to add row
     c1FlexGrid1.Rows.Count += 1;
           
    // Set data
     c1FlexGrid1[c1FlexGrid1.Rows.Count - 1, 1] = "New Row 3";
    
     ' Approach1:
     ' Use the RowCollection.Add method to add row
     Dim r As C1.Win.C1FlexGrid.Row
     r = c1FlexGrid1.Rows.Add()
    
     ' Set data
     r(1) = "New Row 2"
    
     ' Approach2: 
     ' Use AddItem method to add row and set data
     c1FlexGrid1.AddItem("" & vbTab & "New Row 1")
    
     ' Approach3:
     ' Use the RowCollection.Count property to add row
     c1FlexGrid1.Rows.Count += 1
    
     ' Set data
     c1FlexGrid1(c1FlexGrid1.Rows.Count - 1, 1) = "New Row 3"    
    

    Delete Row

    To delete a particular row from the grid, you can use Remove method of the RowCollection class and specify the row you want to delete as its parameter. The RowCollection class also provides RemoveRange method which allows you to remove a range of rows using a single call. Similarly, you can also use RemoveItem method of the C1FlexGrid class to remove a specific row. In the unbound grid, you can reduce the number of rows by changing the value of Count property.

    Following code gives different approaches to delete row from the WinForms FlexGrid.

    // Approach1:
    // Remove second row using RowCollection.Remove method
    c1FlexGrid1.Rows.Remove(2);
    
    // Approach1:
    // Remove second row using the RemoveItem method
    c1FlexGrid1.RemoveItem(2);
    
    // Approach3:
    // Remove 3 rows starting from second using the RemoveRange method
    c1FlexGrid1.Rows.RemoveRange(2, 3);
           
    // Approach4:
    // Remove last row using the RowCollection.Count property 
    c1FlexGrid1.Rows.Count -= 1;                                        
    
     ' Approach1:
     ' Remove second row using RowCollection.Remove method
     c1FlexGrid1.Rows.Remove(2)
    
     ' Approach1:
     ' Remove second row using the RemoveItem method
     c1FlexGrid1.RemoveItem(2)
    
     ' Approach3:
     ' Remove 3 rows starting from second using the RemoveRange method
     c1FlexGrid1.Rows.RemoveRange(2, 3)
    
     ' Approach4:
     ' Remove last row using the RowCollection.Count property 
     c1FlexGrid1.Rows.Count -= 1    
    

    Insert Row

    To insert a row in FlexGrid at a specific location, you can use Insert method of the RowCollection class which lets you specify the position where rows are to be inserted. You can also insert multiple rows in the grid by using the InsertRange method.

    Below code demonstrates how to insert a row at a particular position in the WinForms FlexGrid.

     C1.Win.C1FlexGrid.Row r;
             
    // Approach1:
    // Use the Insert method to insert row at second position
     r = c1FlexGrid1.Rows.Insert(2);
     r[1] = "Inserted row";
      
    // Approach2:
    // Use the InsertRange method to add three rows at second position
    // c1FlexGrid1.Rows.InsertRange(2, 3);                 
    
    Dim r As C1.Win.C1FlexGrid.Row
                                            
     ' Approach1:
     ' Use the Insert method to insert row at second position
     r = c1FlexGrid1.Rows.Insert(2)
     r(1) = "Inserted row"
    
     ' Approach2:
     ' Use the InsertRange method to add three rows at second position
     ' c1FlexGrid1.Rows.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 rows or columns by specifying the DataType property of Row or Column class respectively. Note that when data type is set for both rows and columns, column settings take preference over row settings.

    Use the code below to set data type of a WinForms FlexGrid row.

      C1.Win.C1FlexGrid.Row r;
      r = c1FlexGrid1.Rows.Add();
      r.DataType = typeof(int);
    
     Dim r As C1.Win.C1FlexGrid.Row
     r = c1FlexGrid1.Rows.Add()
     r.DataType = GetType(int)    
    

    Set Fixed Row

    Fixed rows refer to the rows with non-editable cells which are always visible on top of the grid even when user scrolls down the grid. In FlexGrid, fixed rows can be set using Fixed property of the RowCollection class. This property accepts an integer value that specifies the number of rows to be fixed.

    Set fixed rows in the WinForms FlexGrid using the code below.

      // Set three rows as fixed
      c1FlexGrid1.Rows.Fixed = 3;                   
    
    ' Set three rows as fixed
    c1FlexGrid1.Rows.Fixed = 3       
    

    Set Frozen Row

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

    Use the code below to set frozen rows in the WinForms FlexGrid.

      // Set first two rows as frozen 
      c1FlexGrid1.Rows.Frozen = 2;
    
    ' Set first two rows as frozen 
    c1FlexGrid1.Rows.Frozen = 2        
    
    See Also