ComponentOne True DBGrid for WinForms
Data Binding / Adding New Rows to an Unbound Grid
In This Topic
    Adding New Rows to an Unbound Grid
    In This Topic

    You can now easily add new rows to an unbound grid by using the C1TrueDBGrid.NewRow method which creates a new System.Data.DataRow with the same schema as the unbound grid. In the following steps you'll use the C1TrueDBGrid.Rows collection, which gets the DataRowCollection for an unbound grid, and the C1TrueDBGrid.NewRow method to insert a new row into the specified index of an unbound grid.

    Complete the following steps:

    1. Create a new .NET project.
    2. Navigate to the Toolbox and add the C1TrueDBGrid, Label, NumericUpDown, and Button controls to the form.
    3. Set the Button1.Text property to "Add New Row" and the Label1.Text property to "New Row Index".
    4. Arrange the controls on the form similar to the following image:
    5. Switch to Code view and add the following imports (or using) statement to the project:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      imports C1.Win.C1TrueDBGrid
      

      To write code in C#

      C#
      Copy Code
      using C1.Win.C1TrueDBGrid;
      
    6. Add the following code to create the Form_Load event and add data to the grid:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
          ' Add a caption to the grid.
          Me.C1TrueDBGrid1.Caption = "Unbound Grid"
       
          ' Add columns to the grid.
          Me.C1TrueDBGrid1.Columns.Add(New C1.Win.C1TrueDBGrid.C1DataColumn("Col 1", GetType(String)))
          Me.C1TrueDBGrid1.Columns.Add(New C1.Win.C1TrueDBGrid.C1DataColumn("Col 2", GetType(String)))
          Me.C1TrueDBGrid1.Columns.Add(New C1.Win.C1TrueDBGrid.C1DataColumn("Col 3", GetType(String)))
          Me.C1TrueDBGrid1.Columns.Add(New C1.Win.C1TrueDBGrid.C1DataColumn("Col 4", GetType(String)))
       
          ' Call the SetDataBinding method with no arguments.
          Me.C1TrueDBGrid1.SetDataBinding()
       
          ' Populate the grid.
          Dim i As Integer
          For i = 0 To 20 - 1
              Dim s As String = String.Format("Data {0};Data {1};Data {2}; Data {3}", New Object() {i, i, i, i})
              Me.C1TrueDBGrid1.AddRow(s)
          Next i
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void Form1_Load(object sender, EventArgs e)
      {
          // Add a caption to the grid.
          this.c1TrueDBGrid1.Caption = "Unbound Grid"
       
          // Add columns to the grid.
          this.c1TrueDBGrid1.Columns.Add(new C1.Win.C1TrueDBGrid.C1DataColumn("Col 1", typeof(string)));
          this.c1TrueDBGrid1.Columns.Add(new C1.Win.C1TrueDBGrid.C1DataColumn("Col 2", typeof(string)));
          this.c1TrueDBGrid1.Columns.Add(new C1.Win.C1TrueDBGrid.C1DataColumn("Col 3", typeof(string)));
          this.c1TrueDBGrid1.Columns.Add(new C1.Win.C1TrueDBGrid.C1DataColumn("Col 4", typeof(string)));
          
          // Call the SetDataBinding method with no arguments.
          this.c1TrueDBGrid1.SetDataBinding();
          
          // Populate the grid.
          for (int i = 0; i < 20; i++)
          {
              string s = String.Format("Data {0};Data {1};Data {2}; Data {3}", i, i, i, i);
              this.c1TrueDBGrid1.AddRow(s);
          }
      }
      
    7. Add the following code to create the Button_Click event and create a new row at the specified index when the button is clicked:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Dim idx As Integer = CInt(Me.NumericUpDown1.Value)
          ' Create a new row.
          Dim dr As DataRow = Me.C1TrueDBGrid1.NewRow
          dr.Item(0) = "new row"
          ' Add the new row at the selected index.
          Me.C1TrueDBGrid1.Rows.InsertAt(dr, idx)
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void button1_Click(object sender, EventArgs e)
      {
          int idx = (int) this.numericUpDown1.Value;
          // Create a new row.
          DataRow dr = this.c1TrueDBGrid1.NewRow();
          dr[0] = "new row";
          // Add the new row at the selected index.
          this.c1TrueDBGrid1.Rows.InsertAt(dr, idx);
      }
      

    Run the application and observe:

    The form will appear similar to the following:


    Use the arrows to change the number in the New Row Index box and then select the Add New Row button. The new row will appear at the index that you chose:

     

    See Also