ComponentOne True DBGrid for WinForms
True DBGrid for WinForms Task-Based Help / Adding a New Row to C1TrueDBGrid
In This Topic
    Adding a New Row to C1TrueDBGrid
    In This Topic

    To add a new row to C1TrueDBGrid, use the AllowAddNew property and the UpdateData method.

    Complete the following steps:

    1. Set the AllowAddNew property to True.

      In the Designer

      Locate the AllowAddNew property in the Properties window and set it to True.

      In Code

      Add the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.AllowAddNew = True
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.AllowAddNew = true;
      
    2. Move to the last column in the grid by adding the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.MoveLast()
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid.MoveLast();
      
    3. Select the new row:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.Row = Me.C1TrueDBGrid1.Row + 1
      Me.C1TrueDBGrid1.Select()
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.Row = this.c1TrueDBGrid1.Row + 1;
      this.c1TrueDBGrid1.Select();
      
    4. Assign values to the new cells in the first three columns:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.Columns(0).Text = "New Row"
      Me.C1TrueDBGrid1.Columns(1).Text = "New Row"
      Me.C1TrueDBGrid1.Columns(2).Text = "New Row"
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.Columns[0].Text = "New Row";
      this.c1TrueDBGrid1.Columns[1].Text = "New Row";
      this.c1TrueDBGrid1.Columns[2].Text = "New Row";
      
    5. Update the data to the dataset:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.UpdateData()
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.UpdateData();
      

    What You've Accomplished

    In this example, a new row has been added to the C1NWind.mdb:


    There is also a SelectedRows property which points to a collection which contains a reference to all the selected rows in the grid.

    See Also