This quick start will guide you through the steps of creating a simple grid application using the FlexGrid control. Follow the steps below to get started:
This section demonstrates how to create a FlexGrid application in .NET Framework by binding it to an external datasource. You can also create a class to supply data to the grid as shown in the .NET 6 tab. Similarly, you can use below steps to bind FlexGrid to a datasource in .NET 6. However, note that some design-time steps may vary in case of .NET 6.
There are two ways to bind data with the FlexGrid control, either choose to bind at design time or through code at run time.
C# |
Copy Code
|
---|---|
// TODO: This line of code loads data into the 'c1NWindDataSet.Products' table. You can move, or remove it, as needed. this.productsTableAdapter.Fill(this.c1NWindDataSet.Products); |
VB.NET |
Copy Code
|
---|---|
' TODO: This line of code loads data into the 'c1NWindDataSet.Products' table. You can move, or remove it, as needed. Me.ProductsTableAdapter.Fill(Me.c1NWindDataSet.Products) |
To bind the grid through code, first, we need to create a database connection string. Then, use an object of data adapter (in this case, OleDbDataAdapter) to create a query to fetch products from the data table and fill it in the data table which is assigned to the DataSource property of C1FlexGrid class.
C# |
Copy Code
|
---|---|
// bind grid to a data source string conn = GetConnectionString(); OleDbDataAdapter da = new OleDbDataAdapter("select * from products", conn); DataTable dt = new DataTable("Products"); da.Fill(dt); c1FlexGrid1.DataSource = dt; |
VB.NET |
Copy Code
|
---|---|
' bind grid to a data source Dim conn As String = GetConnectionString() Dim da As OleDbDataAdapter = New OleDbDataAdapter("select * from products", conn) Dim dt As DataTable = New DataTable("Products") da.Fill(dt) c1FlexGrid1.DataSource = dt |
Note that the above sample code uses a custom method named GetConnectionString to create a connection string with database.
C# |
Copy Code
|
---|---|
static string GetConnectionString() { string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\ComponentOne Samples\Common"; string conn = @"provider=microsoft.jet.oledb.4.0;data source={0}\c1nwind.mdb;"; return string.Format(conn, path); } |
VB.NET |
Copy Code
|
---|---|
Private Shared Function GetConnectionString() As String Dim path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\ComponentOne Samples\Common" Dim conn = "provider=microsoft.jet.oledb.4.0;data source={0}\c1nwind.mdb;" Return String.Format(conn, path) End Function |
This section walks you through the design-time and run-time steps to configure the grid with some basic settings. These settings and features are explained in detail in the topics below.
Add following code to configure the grid and its columns at run time.
C# |
Copy Code
|
---|---|
c1FlexGrid1.Styles.Fixed.ForeColor = Color.Blue; c1FlexGrid1.Styles.Fixed.Font = new Font("Microsoft Sans serif", 9, FontStyle.Bold); c1FlexGrid1.Dock = DockStyle.Fill; c1FlexGrid1.Cols[6].Format = "c"; |
VB.NET |
Copy Code
|
---|---|
c1FlexGrid1.Styles.Fixed.ForeColor = Color.Blue c1FlexGrid1.Styles.Fixed.Font = New Font("Microsoft Sans serif", 9, FontStyle.Bold) c1FlexGrid1.Dock = DockStyle.Fill c1FlexGrid1.Cols(6).Format = "c" |
This section demonstrates how to create a FlexGrid application in .NET 6 by populating its data from a custom class. You can also use an external data source to supply data to the grid as shown in .NET Framework tab. However, note that some design-time steps given in the aforementioned tab may vary in case of .NET 6. Similarly, you can use below steps to bind FlexGrid to an internal datasource in .NET Framework.
C# |
Copy Code
|
---|---|
// Initialize the control C1FlexGrid flexGrid = new C1FlexGrid(); // Add the control to form this.Controls.Add(flexGrid); |
VB.NET |
Copy Code
|
---|---|
' Initialize the control Dim flexGrid As C1FlexGrid = New C1FlexGrid() ' Add the control to form Me.Controls.Add(flexGrid) |
C# |
Copy Code
|
---|---|
// Create a custom class Product public class Product { static Random _rnd = new Random(); static string[] _names = "Macko|Surfair|Pocohey|Studeby".Split('|'); static string[] _lines = "Computers|Washers|Stoves|Cars".Split('|'); static string[] _colors = "Red|Green|Blue|White".Split('|'); public Product() { Name = _names[_rnd.Next() % _names.Length]; Line = _lines[_rnd.Next() % _lines.Length]; Color = _colors[_rnd.Next() % _colors.Length]; Price = 30 + _rnd.NextDouble() * 1000; Cost = 3 + _rnd.NextDouble() * 300; Discontinued = _rnd.NextDouble() < .2; Introduced = DateTime.Today.AddDays(_rnd.Next(-600, 0)); } public string Name { get; set; } public string Color { get; set; } public string Line { get; set; } public double Price { get; set; } public double Cost { get; set; } public DateTime Introduced { get; set; } public bool Discontinued { get; set; } } |
VB.NET |
Copy Code
|
---|---|
' Create a custom class Product Public Class Product Private Shared _rnd As Random = New Random() Private Shared _names As String() = "Macko|Surfair|Pocohey|Studeby".Split("|"c) Private Shared _lines As String() = "Computers|Washers|Stoves|Cars".Split("|"c) Private Shared _colors As String() = "Red|Green|Blue|White".Split("|"c) Public Sub New() Name = _names(_rnd.[Next]() Mod _names.Length) Line = _lines(_rnd.[Next]() Mod _lines.Length) Color = _colors(_rnd.[Next]() Mod _colors.Length) Price = 30 + _rnd.NextDouble() * 1000 Cost = 3 + _rnd.NextDouble() * 300 Discontinued = _rnd.NextDouble() < .2 Introduced = Date.Today.AddDays(_rnd.[Next](-600, 0)) End Sub Public Property Name As String Public Property Color As String Public Property Line As String Public Property Price As Double Public Property Cost As Double Public Property Introduced As Date Public Property Discontinued As Boolean End Class |
C# |
Copy Code
|
---|---|
// Initialises a List of type Product, where Product is a class type List<Product> _products = new List<Product>(); |
VB.NET |
Copy Code
|
---|---|
' Initialises a List of type Product, where Product is a class type Dim _products As List(Of Product) = New List(Of Product)() |
For
loop and add products to the list.
C# |
Copy Code
|
---|---|
// Initialize for loop and add products to the list for (int i = 0; i <100; i++) { _products.Add(new Product()); } |
VB.NET |
Copy Code
|
---|---|
' Initialize for loop and add products to the list For i As Integer = 0 To 100 - 1 _products.Add(New Product()) Next |
C# |
Copy Code
|
---|---|
// Bind FlexGrid with data
flexGrid.DataSource = _products;
|
VB.NET |
Copy Code
|
---|---|
' Bind FlexGrid with data
flexGrid.DataSource = _products
|
Add following code to configure the grid and its columns at run time.
C# |
Copy Code
|
---|---|
c1FlexGrid1.Styles.Fixed.ForeColor = Color.Blue; c1FlexGrid1.Styles.Fixed.Font = new Font("Microsoft Sans serif", 9, FontStyle.Bold); c1FlexGrid1.Dock = DockStyle.Fill; c1FlexGrid1.Cols[6].Format = "c"; |
VB.NET |
Copy Code
|
---|---|
c1FlexGrid1.Styles.Fixed.ForeColor = Color.Blue c1FlexGrid1.Styles.Fixed.Font = New Font("Microsoft Sans serif", 9, FontStyle.Bold) c1FlexGrid1.Dock = DockStyle.Fill c1FlexGrid1.Cols(6).Format = "c" |