Back to Devexpress

XRBindingCollection.Add(String, Object, String) Method

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xrbindingcollection-dot-add-x28-system-dot-string-system-dot-object-system-dot-string-x29.md

latest10.1 KB
Original Source

XRBindingCollection.Add(String, Object, String) Method

Creates an XRBinding object and adds it to the collection.

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public XRBinding Add(
    string propertyName,
    object dataSource,
    string dataMember
)
vb
Public Function Add(
    propertyName As String,
    dataSource As Object,
    dataMember As String
) As XRBinding

Parameters

NameTypeDescription
propertyNameString

The name of the control’s property to bind.

| | dataSource | Object |

An object that represents the data source.

| | dataMember | String |

A string containing a navigation path resolving to the property of an object.

|

Returns

TypeDescription
XRBinding

An XRBinding object.

|

Example

The following two methods demonstrate how to add and remove an XRBinding object from the control’s collection of data bindings. Data bindings are added in two different ways. For the lbUnitPrice label the binding is first created and then added, for the lbProductName label the binding is created and added simultaneously.

Note that for this example to work correctly, a report must contain the dsProducts dataset, which should use data in the Products table (“SELECT Products.* FROM Products”) from the sample Northwind database.

csharp
using DevExpress.XtraReports.UI;
// ...

public class XtraReport1 : DevExpress.XtraReports.UI.XtraReport {
// ...

    public void AddBindings() {
        // Create a data binding.
        XRBinding binding = new XRBinding("Text", dsProducts1, "Products.UnitPrice");

        // Add the created binding to the binding collection of the lbUnitPrice label.
        lbUnitPrice.DataBindings.Add(binding);

        // Create and add the binding to the binding collection of the lbProductName label.
        lbProductName.DataBindings.Add("Text", dsProducts1, "Products.ProductName");
    }

    public void RemoveBinding(XRControl control, string propertyName) {
        // Get the data binding specified by the control's property name.
        XRBinding binding = control.DataBindings[propertyName];

        // Remove the data binding from the control's binding collection.
        if(binding != null)
            control.DataBindings.Remove(binding);
    }

}
vb
Imports DevExpress.XtraReports.UI
' ...

Public Class XtraReport1
    Inherits DevExpress.XtraReports.UI.XtraReport
' ...

    Public Sub AddBindings()
        ' Create a data binding.
        Dim binding As New XRBinding("Text", dsProducts1, "Products.UnitPrice")

        ' Add the created binding to the binding collection of the lbUnitPrice label.
        lbUnitPrice.DataBindings.Add(binding)

        ' Create and add the binding to the binding collection of the lbProductName label.
        lbProductName.DataBindings.Add("Text", dsProducts1, "Products.ProductName")
    End Sub

    Public Sub RemoveBinding(ByRef control As XRControl, ByRef propertyName As String)
        ' Get the data binding specified by the control's property name.
        Dim binding = control.DataBindings(propertyName)

        ' Remove the data binding from the control's binding collection.
        If (Not (binding Is Nothing)) Then
            control.DataBindings.Remove(binding)
        End If
    End Sub
End Class

The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(String, Object, String) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

reporting-winforms-bind-mdb-with-select-query/CS/RuntimeBindingToMdbDatabase/Form1.cs#L64

csharp
if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings) {
    label1.DataBindings.Add("Text", null, "selectQuery.CategoryName");
    label2.DataBindings.Add("Text", null, "selectQuery.ProductCount");

reporting-winforms-filter-report-data-query/CS/QueryParametersRuntime/XtraReport1.cs#L49

csharp
if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings) {
    xrLabel1.DataBindings.Add("Text", ds, "Products.CategoryID");
    xrLabel2.DataBindings.Add("Text", ds, "Products.ProductName");

asp-net-web-forms-grid-create-report-based-on-grid-layout/CS/WebApplication1/ReportHelper.cs#L70

csharp
label.SizeF = ((XRTableCell)detailsInfo[col]).SizeF;
label.DataBindings.Add("Text", null, ((GridViewDataColumn)col).FieldName);
label.Summary = new XRSummary() { Running = SummaryRunning.Group };

reporting-winforms-bind-to-csv-file/CS/BindingReportToCsvFile/Form1.cs#L53

csharp
if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings)
    label.DataBindings.Add("Text", null, "CategoryName");
else label.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[CategoryName]"));

asp-net-mvc-grid-create-report-based-on-grid-layout/CS/E4755/Models/ReportHelperMVC.cs#L54

csharp
label.SizeF = ((XRTableCell) detailsInfo[col]).SizeF;
label.DataBindings.Add("Text", null, col.FieldName);
label.Summary = new XRSummary() {

reporting-winforms-bind-mdb-with-select-query/VB/RuntimeBindingToMdbDatabase/Form1.vb#L56

vb
If DesignerOptions.DataBindingMode = DataBindingMode.Bindings Then
    label1.DataBindings.Add("Text", Nothing, "selectQuery.CategoryName")
    label2.DataBindings.Add("Text", Nothing, "selectQuery.ProductCount")

reporting-winforms-filter-report-data-query/VB/QueryParametersRuntime/XtraReport1.vb#L47

vb
If Settings.Default.UserDesignerOptions.DataBindingMode = DataBindingMode.Bindings Then
    xrLabel1.DataBindings.Add("Text", ds, "Products.CategoryID")
    xrLabel2.DataBindings.Add("Text", ds, "Products.ProductName")

asp-net-mvc-grid-create-report-based-on-grid-layout/VB/E4755/Models/ReportHelperMVC.vb#L53

vb
label.SizeF = (CType(detailsInfo(col), XRTableCell)).SizeF
label.DataBindings.Add("Text", Nothing, col.FieldName)
label.Summary = New XRSummary() With {.Running = SummaryRunning.Report}

asp-net-web-forms-grid-create-report-based-on-grid-layout/VB/WebApplication1/ReportHelper.vb#L72

vb
label.SizeF = DirectCast(detailsInfo(col), XRTableCell).SizeF
label.DataBindings.Add("Text", Nothing, CType(col, GridViewDataColumn).FieldName)
label.Summary = New XRSummary() With {.Running = SummaryRunning.Group}

reporting-winforms-group-at-runtime/VB/DataGrouping/Form1.vb#L48

vb
If Settings.Default.UserDesignerOptions.DataBindingMode = DataBindingMode.Bindings Then
    labelGroup.DataBindings.Add("Text", Nothing, "customQuery.CategoryName")
    labelDetail.DataBindings.Add("Text", Nothing, "customQuery.ProductName")

See Also

Bind Report Controls to Data Using Expression Bindings

XRBindingCollection Class

XRBindingCollection Members

DevExpress.XtraReports.UI Namespace