ComponentOne List for WinForms
Data Binding / Bound Mode
In This Topic
    Bound Mode
    In This Topic

    The List supports data binding to commonly used data sources such as ObservableCollection, IList <T>, List<T>, ADO.NET objects such as DataSet, DataTable, etc. In the bound mode, the list fetches its data from the underlying data source. Additionally, it lets multiple data consumers be connected to a single data provider in a synchronized manner.

    For detailed information on how to bind List to a data source, refer to the following links:

    Operations in Bound Mode

    List lets you perform operations in bound mode. Let us discuss some common operations that can be performed in bound mode in the List control.

    Add Unbound Column in Bound Mode

    Normally, List automatically displays data from bound database fields. However, you may need to augment the set of fields present in your layouts with columns derived from database fields, or columns which are unrelated (or only loosely related) to database information. For example, if your database contains a Balance field, you may instead want to display two columns, Credit and Debit. To accomplish this task, you can use unbound columns to display data that does not exist in the data source.

    List provides Add and Insert methods in the C1DataColumnCollection class to add an unbound column to the list. While the Add method adds a column at the end, the Insert method lets you specify the position where you want to add a new column.

    Use the following code to add an unbound column to the list.

    C#
    Copy Code
    C1DataColumn dc = new C1DataColumn();
    //sets the caption of unbound column
    dc.Caption = "Available Value";
    //add new unbound column
    c1List1.Columns.Add(dc);
    

    Similarly, you can create multiple unbound columns in List by using the Add method.

    Set Values in Unbound Column

    To define values in the unbound column of bound list, you need to use UnboundColumnFetch event of the C1List class. This event automatically gets fired when the control needs to display the value of cells in an unbound mode.

    This example uses the sample created in the Quick Start topic. Here, the product of two column fields "UnitsInStock" and "UnitPrice" is displayed in the unbound column "Available Value" using the UnboundColumnFetch event.

    C#
    Copy Code
    private void C1List1_UnboundColumnFetch(object sender, UnboundColumnFetchEventArgs e)
    {
        if (c1List1.Columns[e.Col].Caption == "Available Value")
        {
            var units = int.Parse(c1List1.Columns["UnitsInStock"].CellText(e.Row));
            var price = int.Parse(c1List1.Columns["UnitPrice"].CellText(e.Row));
            e.Value = (units * price).ToString();
        }
    
    }
    

    Similarly, the UnboundColumnFetch event can also be used to populate more than one unbound columns at the same time. For example, in the following code, we populate two unbound columns, "AvailableValue" and "Profit".

    C#
    Copy Code
    private void C1List1_UnboundColumnFetch(object sender, UnboundColumnFetchEventArgs e)
    {
       //pupulates the unbound column using if-else condition
        if (this.c1List1.Columns[e.Col].Caption == "AvailableValue")
        {
            var units1 = int.Parse(c1List1.Columns["UnitsInStock"].CellText(e.Row));
            var price = int.Parse(c1List1.Columns["UnitPrice"].CellText(e.Row));
            //sets the value
            e.Value = (units1 * price).ToString();
        }
        if (this.c1List1.Columns[e.Col].Caption == "Profit")
        {
            var price = int.Parse(c1List1.Columns["UnitPrice"].CellText(e.Row));
            //sets the value
            e.Value = (((4* price)/100)+price).ToString();
    
        }
    }