Search code examples
c#winformsdevexpressxtrareport

Two fields values bind to one xrlabel at runtime


My report is bind with DataTable. I want to show two field values in my label.

private void Form1_Load(object sender, EventArgs e)
{
    using (rptCustomer rpt = new rptCustomer())
    {
        rpt.DataSource = CreateTestData();
        rpt.DataMember = CreateTestData().TableName;

        rpt.xrLabel.Text = "[CustomerID] - [CustomerName]"; // it's doesn't work

        //rpt.xrLabel.DataBindings.Add("Text", null, "Customer.CustomerID"); -- it's work

        rpt.xrCustomerID.DataBindings.Add("Text", null, "Customer.CustomerID");
        rpt.xrCustomerName.DataBindings.Add("Text", null, "Customer.CustomerName");
        rpt.xrCustomerAddress.DataBindings.Add("Text", null, "Customer.Address");
        rpt.ShowPreviewDialog();
    }
}

public DataTable CreateTestData()
{
    DataTable dt = new DataTable("Customer");
    dt.Columns.Add("CustomerID");
    dt.Columns.Add("CustomerName");
    dt.Columns.Add("Address");

    for (int i = 1; i <= 10; i++)
    {
        DataRow row = dt.NewRow();
        row[0] = i;
        row[1] = "Customer " + i;
        row[2] = "Address " + i;
        dt.Rows.Add(row);
    }

    return dt;
}

This is my result report.
[CustomerID] - [CustomerName] must be 1 - Customer 1.

Here my result!


Solution

  • You need to use DataSet object as data source of your report.
    Here is example:

    using (rptCustomer rpt = new rptCustomer())
    {
        var dataSet = new DataSet("TestDataSet");
        var table = CreateTestData();
    
        dataSet.Tables.Add(table);
    
        rpt.DataSource = dataSet;
        rpt.DataMember = table.TableName;
    
        rpt.xrLabel.Text = "[CustomerID] - [CustomerName]"; // Suddenly, it's work.
    
        //rpt.xrLabel.DataBindings.Add("Text", null, "Customer.CustomerID"); -- It's work too.
    
        rpt.xrCustomerID.DataBindings.Add("Text", null, "Customer.CustomerID");
        rpt.xrCustomerName.DataBindings.Add("Text", null, "Customer.CustomerName");
        rpt.xrCustomerAddress.DataBindings.Add("Text", null, "Customer.Address");
        rpt.ShowPreviewDialog();
    }