如何在c#中使用数据源作为datagrid在rdlc报表中显示数据

时间:2018-04-17 06:14:32

标签: c# datagrid rdlc

我希望使用数据网格在rdlc报告中获得准确的输出。如何将报表数据源绑定为数据网格?并且还需要在报告中显示数据网格标题。我正在使用c#。

1 个答案:

答案 0 :(得分:0)

 Here is an example i created for you... You can replace the dummy customer 
 list with your desired data. 


    public class Customer
    {

        public string Name { get; set; }
        public string LastName { get; set; }
    }


    public Form1()
    {
        InitializeComponent();
        var customer = new List<Customer>();
        customer.Add(new Customer { Name = "A", LastName = "B" });
        customer.Add(new Customer { Name = "C", LastName = "D" });
        dataGridView1.DataSource = customer;

    }


    private void button1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        foreach (DataGridViewColumn col in dataGridView1.Columns)
        {
            dt.Columns.Add(col.Name);
        }

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataRow dRow = dt.NewRow();
            foreach (DataGridViewCell cell in row.Cells)
            {
                dRow[cell.ColumnIndex] = cell.Value;
            }
            dt.Rows.Add(dRow);
        }
        try
        {
            reportViewer1.ProcessingMode = 
            Microsoft.Reporting.WinForms.ProcessingMode.Local;
            reportViewer1.LocalReport.ReportPath = @"Your Path to the Report File";

            Microsoft.Reporting.WinForms.ReportDataSource dataSource = new 
            Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dt);
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(dataSource);
            reportViewer1.LocalReport.Refresh();
            reportViewer1.RefreshReport();
        }
        catch
        {

        }
   }