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:
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; |
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); } } |
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); } |
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: