How to bind dynamically datasource to reportviewer on windows forms c#

后端 未结 3 1020
说谎
说谎 2020-12-18 15:35

I have created windows form that acts as report loader. I have created also two RDLC reports by report wizard and it automatically created dataset for those two reports. Now

相关标签:
3条回答
  • 2020-12-18 16:05

    To force Visual Studio to create automatically the bindingsource, you have to create the report (.rdlc) first. With a datasource linked, when you add the component (reportviewer) and link the component with the name of the report as shown in your image, once you select the report, it will automatically create a binding source with the name that you use when you created the design.

    0 讨论(0)
  • 2020-12-18 16:07

    I spent some time on the same problem. I'm using VS 2013 where the dataset is connected through an automatically-created BindingSource in the report form. If you create the report and add all the required elements, everything works fine. If you (say) add a table later, the BindingSources get out-of-step, or aren't created.

    Edit the report form Design view, click on the ReportViewer object and use the little 'Tasks' arrowhead in the top r.h. corner. Either select 'Rebind Data Sources' or 'Choose Data Sources' to check they are correctly bound.

    0 讨论(0)
  • 2020-12-18 16:27

    OK, trying to figure it out, yesterday I found a solution which was acceptable to me, so I wanted to share with others:

    1.st you need to create a class model with properties so it will be added in dataset: example:

    namespace NavisReportLoader.App_Data
    {
        public class ExtraMoveModel
        {
            public string EventType { get; set; }
            public int EventCount { get; set; }
            public int Num20 { get; set; }
            public int Num40 { get; set; }
            public int Num45 { get; set; }
            public int TEU { get; set; }
            public float Cargo { get; set; }
            public float Tare { get; set; }
            public float Total { get; set; }
        }
    }
    

    after that you need to create plain simple class for connecting to the database and call the stored procedure, pass the parameters and read using data reader output. In my example, I have added this into a list and enumerate my model:

    example:

    public class ExtraMoveDataSet
    {
        string connectionString = @"Data Source=sampleDB; Initial Catalog=test; User Id=sa; Password=test";
        public IEnumerable<ExtraMoveModel> extraMove(DateTime dateFrom, DateTime dateTo)
        {
            var tempList = new List<ExtraMoveModel>();
            //string connectionString = @"Data Source=nsqltest; Initial Catalog=sparcsn4; User Id=sa; Password=lo02Nova";
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("AGCT_ServiceEventReport", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@dateFrom", dateFrom);
            cmd.Parameters.AddWithValue("@dateTo", dateTo);
            conn.Open();
    
            using (var dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    var temp = new ExtraMoveModel();
                    temp.EventType = dr["event_type"].ToString();
                    temp.EventCount = Convert.ToInt32(dr["CNT"]);
                    temp.Num20 = Convert.ToInt32(dr["NUM20"]);
                    temp.Num40 = Convert.ToInt32(dr["NUM40"]);
                    temp.Num45 = Convert.ToInt32(dr["NUM45"]);
                    temp.TEU = Convert.ToInt32(dr["TEU"]);
                    temp.Cargo = float.Parse(dr["Cargo"].ToString(),new CultureInfo("hr-HR"));
                    temp.Tare = float.Parse(dr["Tare"].ToString(),new CultureInfo("hr-HR"));
                    temp.Total = float.Parse(dr["Total"].ToString(),new CultureInfo("hr-HR"));
                    tempList.Add(temp);
                }
            }
            conn.Close();
            return tempList;
        }
    
    1. step is to create dataset that will have same name as properties from a model.

    enter image description here

    1. step create report on which you will bind dataset.

    enter image description here

    1. finally you can add it to the reportViewer1

      private void ReportForm_Load(object sender, EventArgs e)
      {
          ExtraMoveDataSet emDS = new ExtraMoveDataSet();
          if (idRep.Equals("extraMove"))
          {
              ReportParameter[] param = new ReportParameter[2];
              param[0] = new ReportParameter("date1", dat1);
              param[1] = new ReportParameter("date2", dat2);
              //string path = Directory.GetCurrentDirectory();
              //string replace = path.Replace("\\bin\\Debug", "") + "\\App_Data\\"+"ReportExtraMove.rdlc";
              var ret = emDS.extraMove(d1, d2);
              ReportDataSource rds = new ReportDataSource("extraMove", ret.ToArray());
              this.reportViewer1.LocalReport.DataSources.Add(rds);
              //this.reportViewer1.LocalReport.ReportPath = replace;
              this.reportViewer1.LocalReport.ReportEmbeddedResource = "NavisReportLoader.App_Data.ReportExtraMove.rdlc";
              this.reportViewer1.LocalReport.SetParameters(param);
              this.reportViewer1.RefreshReport();
          }
      }
      

    I hope this will help other to speed things up.

    cheers!

    0 讨论(0)
提交回复
热议问题