两个字段值在运行时绑定到一个xrlabel

时间:2015-10-27 12:10:57

标签: c# winforms devexpress xtrareport

我的报告与DataTable绑定。我想在我的标签中显示两个字段值。

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;
}

这是我的结果报告 [CustomerID] - [CustomerName]必须为1 - Customer 1

Here my result!

1 个答案:

答案 0 :(得分:0)

您需要使用DataSet对象作为报告的数据源 这是一个例子:

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();
}