C#使用属性进行之传递查询报表
最近想做一个Form2 写数据传递到Form1的报表筛选中,进行查询;用了一个比较笨的办法.
Form1 代码:
namespace WindowsFormsApplication7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string fdate ; public string Fdate { get { return fdate; } set { fdate = value; } } private string tdate; public string Tdate { get { return tdate; } set { tdate = value; } } private string cname; public string Cname { get { return cname; } set { cname = value; } } private DataTable LoadData() { SqlConnection Conn = new SqlConnection("Server=Jim;Database=defectiveData;User id=sa;password=colday"); SqlCommand cmd = new SqlCommand(string.Format("select * from DEFDATA where DEFDATE between '" + fdate + "' and '" + tdate + "'and DEFSTOREN ='" + cname + "'", Conn)); SqlDataAdapter adp = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adp.SelectCommand.Connection = Conn; adp.Fill(ds); return ds.Tables[0]; } private void reportViewer1_Load(object sender, EventArgs e) { reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("defective_DEFDATA", LoadData())); reportViewer1.LocalReport.ReportEmbeddedResource = "WindowsFormsApplication7.Report2.rdlc"; reportViewer1.RefreshReport(); } }
Form2代码:
namespace WindowsFormsApplication7 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form1 fm1 = new Form1(); fm1.Fdate = fdate.Value.Date.ToString(); fm1.Tdate = tdate.Value.Date.ToString(); fm1.Cname = comboBox1.Text.ToString(); fm1.Show(); } } }