如何将C#表单文本框值传递给Reportviewer中的文本框 [英] how to pass C# form textbox value to textbox in Reportviewer

查看:180
本文介绍了如何将C#表单文本框值传递给Reportviewer中的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2010.我有一个带有文本框和reportviewer的表单.

在我的表单文本框中,客户端插入一个值名称Invoicenumber,然后当用户按下按钮时,reportviewer必须生成,并且在reportviewer文本框中,我希望显示发票编号.

我为发票编号创建了一个参数:Parameter!invoicenumber,并在我的reportviewer表达式中对其进行了调用.我想念什么
这是我的表格代码

Im using VS 2010. I have a form with a textbox and reportviewer on it.

In my form textbox client inserts a value name Invoicenumber then when user press button the reportviewer must generate and in reportviewer textbox i want invoice number to display.

I created a parameter for invoicenumber: Parameter!invoicenumber and called it in my reportviewer expression. What am i missing
This is my Form code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Windows.Forms.PropertyGridInternal;


namespace WindowsFormsApplication1
{
    public partial class Form12 : Form
    {
        string ConnectionString = @"Data Source=Admin-PC\SQLEXPRESS;Initial Catalog=Couriers;Integrated Security=True";
        SqlCommand com;
        // SqlDataAdapter da;
        // DataSet ds;
        String str;
        DataTable dt;

        public Form12()
        {
            InitializeComponent();
        }

        private void Form12_Load(object sender, EventArgs e)
        {
            
            // TODO: This line of code loads data into the 'couriersDataSet20.Cycle' table. You can move, or remove it, as needed.
            this.cycleTableAdapter.Fill(this.couriersDataSet20.Cycle);
            SqlConnection sqlConn = new SqlConnection(ConnectionString);
            sqlConn.Open();
            str = "SELECT * from Cycle";
            SqlCommand com = new SqlCommand(str, sqlConn);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds, "Cycle");
            dt = ds.Tables["Cycle"];
            sqlConn.Close();
            comboBox1.DataSource = ds.Tables["Cycle"];
            comboBox1.DisplayMember = "CycleNumber";
            comboBox1.Text = "select";
            textBox3.Text = 
            textBox4.Text = "";
            


          //  this.reportViewer1.LocalReport.ReportEmbeddedResource = "Report2.rdlc";
           // SqlParameter sp = new SqlParameter("invoicenumber", this.textBox2.Text);
            //this.reportViewer1.RefreshReport();


            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // TODO: This line of code loads data into the 'DataSet1.Waybills' table. You can move, or remove it, as needed.
            this.WaybillsTableAdapter.Fill(this.DataSet1.Waybills, textBox1.Text, textBox3.Text, textBox4.Text);

            Random random = new Random();

            int num = random.Next(1, 10000);

            textBox2.Text= Convert.ToString(num);
            
         

        	    new Microsoft.Reporting.WinForms.ReportParameter("invoicenumber", textBox2.Text);
                                     
          

            this.reportViewer1.RefreshReport();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection sqlConn = new SqlConnection(@"Data Source=Admin-PC\SQLEXPRESS;Initial Catalog=Couriers;Integrated Security=True");
            sqlConn.Open();
            str = "SELECT * from Cycle where CycleNumber='" + comboBox1.Text.Trim() + "'";
            com = new SqlCommand(str, sqlConn);
            SqlDataReader reader = com.ExecuteReader();

            if (reader.Read())
            {
                textBox3.Text = reader["StartDate"].ToString();
                textBox4.Text = reader["EndDate"].ToString();
            }
            sqlConn.Close();
            reader.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {
            Random random = new Random();

            int num = random.Next(1, 10000);

            label4.Text = Convert.ToString(num);
        }
    }
}


如您所见,我的文本框2生成一个我要用作发票号的随机数.

但是,现在的问题是我不确定如何在我的reportviewer中显示textbox2输入.


As u see my textbox 2 generates a random number that i want to use as invoice number.

But now the problem is im not sure how to display textbox2 input into my reportviewer

推荐答案

http://go4answers.webhost4life.com/Example/passing-variable-textbox-reportviewer-41433.aspx[^]


see this link




首先,您生成发票编号的方法不正确.您不应该在C#中使用逻辑生成发票编号.相反,我建议您在数据库中使用 AutoIncrement Number 生成发票编号.或者,如果您有复杂的InvoiceNumber,则可以创建一个 SQL函数.

第二个问题是在您的connectionString位置中,您不应该对连接字符串进行硬编码.使用配置文件放置您的connectionString.

现在,您的问题将变得很容易,当用户保存发票并按打印按钮时,您可以获得发票编号表单数据库.

最后一个建议,您的代码应该整洁干净.

希望这对您有帮助,
谢谢
-Amit Gajjar
Hi,

First your method of generating Invoice number is incorrect. You should not use logic for generating Invoice number in C#. Instead i suggest you to use AutoIncrement Number in database to generate your invoice Number. Or if you have complex InvoiceNumber then you can create one SQL Function.

Second problem is in your connectionString location, You should not hardcode your connectionString. Use Configuration file to place your connectionString.

Now your problem will be easy, When user Save the Invoice and Press print button you can get Invoice Number form database.

And very last suggestion, Your code should be neat and clean.

Hope this helps you,
Thanks
-Amit Gajjar


这篇关于如何将C#表单文本框值传递给Reportviewer中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆