Combo can be bound to commonly used data sources such as ObservableCollection, IList <T>, List<T>, ADO.NET objects such as DataSet, DataTable, etc. In the bound mode, it fetches its data from the underlying data source.
Let us explore how to perform data binding using the Combo control.
Follow the given steps to create a simple Combo application in .NET Edition by populating its data from a custom class.
C# |
Copy Code
|
---|---|
public class Product { // Create a custom 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; } } |
C# |
Copy Code
|
---|---|
// Initialises a List of type Product, where Product is a class type List<Product> _products = new List<Product>(); |
C# |
Copy Code
|
---|---|
// Initialize for loop and add products to the Combo for (int i = 0; i < 10; i++) { _products.Add(new Product()); } |
C# |
Copy Code
|
---|---|
// Bind C1Combo with data
c1Combo1.DataSource = _products;
|
Add the following code to initialize and add the Combo control to the Form at runtime.
C# |
Copy Code
|
---|---|
// Initialize the control C1Combo c1Combo1 = new C1Combo(); // Add the control to form this.Controls.Add(c1Combo1); |
Follow the given steps to create a simple Combo application in .NET Framework.
There are two ways to bind data with the Combo control, either choose to bind at design time or through code at run time.
The above steps add a dataset, and a connection string to your project. With this, the following code gets generated automatically to fill the dataset:
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); |
To bind the Combo control through code, follow these steps.
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); } |
C# |
Copy Code
|
---|---|
// bind combo to a data source string conn = GetConnectionString(); OleDbDataAdapter da = new OleDbDataAdapter("select * from products", conn); DataTable dt = new DataTable("Products"); da.Fill(dt); c1Combo1.DataSource = dt; |
Add the following code to initialize and add the Combo control to the Form at runtime.
C# |
Copy Code
|
---|---|
// Initialize the control C1Combo c1Combo1 = new C1Combo(); // Add the control to form this.Controls.Add(c1Combo1); |