如何在我的报告中显示我的组合框值?

时间:2014-12-18 20:56:14

标签: c# winforms parameters combobox reportviewer2008

我有一个带有combobox和datetimepicker的表单。我希望用户从组合框中选择一个值并选择一个日期,该值需要作为参数捕获并显示在报告上,其中日期等于所选日期,而itempiececode等于所选的组合框值。

参与的作品

Form-Piece Code.cs 报告 - Report6.rdlc Form Piece Code.cs上的ReportViewer - reportviewer1

如果您仍然不明白我的问题,请点击 - > Picture

突出显示的黄色是我需要的。

守则我太远了

填写报告查看器

private void PieceCode_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'CorsicanaNetWeightDataSet9.CandyPieceSize' table. You can move, or remove it, as needed.
    this.CandyPieceSizeTableAdapter.Fill(this.CorsicanaNetWeightDataSet9.CandyPieceSize);
    // TODO: This line of code loads data into the 'CorsicanaNetWeightDataSet4.PieceDimensionMasterDataUpdate' table. You can move, or remove it, as needed.
    this.PieceDimensionMasterDataUpdateTableAdapter.Fill(this.CorsicanaNetWeightDataSet4.PieceDimensionMasterDataUpdate);
    // TODO: This line of code loads data into the 'corsicanaNetWeightDataSet4piece.Item_Piece' table. You can move, or remove it, as needed.
    this.item_PieceTableAdapter.Fill(this.corsicanaNetWeightDataSet4piece.Item_Piece);
    PieceReport pr = new PieceReport();
    reportViewer1.Visible = false;
    this.reportViewer1.RefreshReport();
}

要填充Combobox,我使用了datasource和displaymemeber属性 **我使用报告向导生成report6 **

我在大多数程序中都使用了GUI,所以没有太多代码可以共享

我需要的摘要 我需要将combobox1.selectedtext设置为report6上的参数,并将datetimepicker.text =设置为另一个参数,以便我可以在reportviewer中显示它?或者如果该计划有任何其他解决方法。

我在这里完全诚实,请不要将此作为主题关闭,或者让我停下来,因为我提供了我所有的理解,请帮助我,因为这是我项目的最后一部分,我我真的很想在假期之前完成它并与家人共度时光,感谢您的帮助

尝试使用代码,GUI有时不会删除它

namespace CorsicanaNetWeightProgram
{
    public partial class PieceCode : Form
    {

        public string constr = "Data Source=KCMJF1XTR1\\SQLEXPRESS;Initial Catalog=CorsicanaNetWeight;Integrated Security=True";

        public reportfiller fullpiecedetails;

        public piecedescription piece;


        PieceReport pr = new PieceReport(); 
        public PieceCode()
        {
            InitializeComponent();
        }

        private void PieceCode_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'CorsicanaNetWeightDataSet9.CandyPieceSize' table. You can move, or remove it, as needed.

            // TODO: This line of code loads data into the 'corsicanaNetWeightDataSet4piece.Item_Piece' table. You can move, or remove it, as needed.
           // this.item_PieceTableAdapter.Fill(this.corsicanaNetWeightDataSet4piece.Item_Piece);
            PieceReport pr = new PieceReport();
            reportViewer1.Visible = false;
            piece = new piecedescription();
            fillpiece();



            this.reportViewer1.RefreshReport();
        }

        private void fillpiece()
        {
            try
            {
            using(MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
            {
                connection.Open ();
                using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT Item + '-' + Description AS PieceDescription FROM dbo.[Piece Dimension Master Data]" ,connection))
                {
                  MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
                        myadapter.SelectCommand = command;
                        myadapter.Fill(piece, "DataTable1");
                comboBox1.DataSource = piece.DataTable1;
                comboBox1.ValueMember = "PieceDescription";
                comboBox1.DisplayMember = "PieceDescription";


        }
            }
            }

            catch (Exception) { /*Handle error*/ }
            }


        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {          

        }

        private void button1_Click(object sender, EventArgs e)
        { 


            reportViewer1.Visible = true;
            {
                using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
                {
                    fullpiecedetails = new reportfiller();
                    connection.Open();
                    //MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT * From EtimePunchDetail WHERE (EmpID = @empid) And  (Paygroup = @paygroup) And (TransDate >= @fromdate) And (TransDate <= @todate)", connection);
                    MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT * FROM dbo.[Piece Dimension Master Data] WHERE Item + '-' + Description  = @piecedesc", connection);
                    {

                        MSSQL.SqlParameter parmEmp = new MSSQL.SqlParameter();
                        parmEmp.ParameterName = "@piecedesc";
                        parmEmp.Value = comboBox1.SelectedValue;
                        command.Parameters.Add(parmEmp);



                        MSSQL.SqlDataAdapter EtimePunchDetailTableAdapter = new System.Data.SqlClient.SqlDataAdapter();
                        EtimePunchDetailTableAdapter.SelectCommand = command;
                        EtimePunchDetailTableAdapter.Fill(fullpiecedetails, "DataTable1");



                        string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
                        string reportPath = exeFolder + @"\Report6.rdlc";



                        reportViewer1.LocalReport.ReportPath = reportPath;

                        reportViewer1.LocalReport.DataSources.Clear();

                        reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("reportfiller_DataTable1", fullpiecedetails));
                        reportViewer1.RefreshReport();
                        reportViewer1.Refresh();
                        reportViewer1.Visible = true;
                   }
                }
            }

        }

我收到以下错误&#34;价值不在预期范围内。&#34;在代码reportViewer1.LocalReport.DataSources.Add的这一部分(新的Microsoft.Reporting.WinForms.ReportDataSource(&#34; reportfiller_DataTable1&#34;,fullpiecedetails));

2 个答案:

答案 0 :(得分:1)

假设您的日期参数名称是DateParameter。要设置值,我将执行以下操作:

        ReportParameter rpDateParameter = new ReportParameter();
        rpDateParameter.Name = "DateParameter";
        rpDateParameter.Values.Add(dateTimePicker1.Value.ToString());
        reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rpDateParameter });

        // Refresh the report
        reportViewer1.RefreshReport();

答案 1 :(得分:0)

我创建了一个新表单,添加了一个新报表,新数据集并通过GUI完成了所有操作 - 使用向导生成报表。

我将数据集编辑为以下代码

SELECT Item, Description, [Piece Category], Shape, [Deposit Weight], [Center Weight], [Center Weight Constant], [Coating Weight], [Coating Weight Constant], [Decorations Weight], [Inclusions Weight], [Center Diameter_LL], [Center Diameter_UL], [Center Length_LL], [Center Length_UL], [Center Width_LL], [Center Width_UL], [Center Height_LL], [Center Height_UL], [Center Weight Variation], [Coated Diameter_LL], [Coated Diameter_UL], [Coated Length_LL], [Coated Length_UL], [Coated Width_LL], [Coated Width_UL], [Coated Height_LL], [Coated Height_UL], [Coated Weight Variation], Item + '-' + Description AS [PieceDescription] FROM [Piece Dimension Master Data] WHERE **Item + '-' + Description  = @piece**

然后在报告中添加了一个包含所有必需列的表

GUI已为我生成此代码

namespace CorsicanaNetWeightProgram
{
    public partial class PieceCodepc : Form
    {
        public PieceCodepc()
        {
            InitializeComponent();
        }

        private void PieceCodepc_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataSet1.DataTable1' table. You can move, or remove it, as needed.
            this.dataTable1TableAdapter.Fill(this.dataSet1.DataTable1);

        }

        private void button1_Click(object sender, EventArgs e)
        {

            // TODO: This line of code loads data into the 'empty.Piece_Dimension_Master_Data' table. You can move, or remove it, as needed.
            this.Piece_Dimension_Master_DataTableAdapter.Fill(this.empty.Piece_Dimension_Master_Data, comboBox1.SelectedValue.ToString());

            this.reportViewer1.RefreshReport();


        }
    }
}

我所做的只是将comboBox1.SelectedValue.ToString()传递给DataTableAdapter并且IT工作!!!!!