XtraReport三动态数据绑定

代码还用上一节的,把Report的Datasource去掉。各个绑定的字段也去掉,有了第二节的基础,现在看这个就不难了,无非就是传到report一个数据集,在把这个数据集绑定到各控件里 清空details里的Cell的值,各cell改成数据库对应列的名字方便绑定

XReport 代码如下 作用就是绑定一下数据

  

 public partial class XtraReport3 : DevExpress.XtraReports.UI.XtraReport
    {
        public XtraReport3()
        {
            InitializeComponent();
        }
        //重构一个构造函数  
        public XtraReport3( DataTable dt)
        {

            InitializeComponent();
            //报表的数据源来自数据库的table
            this.DataSource = dt;
            //lebel绑定数据库text 中ID
            this.xrLabel1.DataBindings.Add("text",dt,"id");
            this.xrTableCell1.DataBindings.Add("text",dt,"id");
        
        }
    }
}

Form1代码如下

  private void button1_Click(object sender, EventArgs e)
        {
           

            string constr = "serer=192.168.100.222;uid=sa;pwd=Metel@1989;database=text";
            SqlConnection con = new SqlConnection(constr);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("select * from book", con);
                DataTable dt = new System.Data.DataTable();
                da.Fill(dt);
                XtraReport3 report = new XtraReport3(dt);
                report.Landscape = false;
                documentViewer1.DocumentSource = report;
                report.CreateDocument();


            }
            catch (Exception)
            {

                throw;
            }
            finally { con.Close(); }
        }

猜你喜欢

转载自www.cnblogs.com/xiaowie/p/8970709.html
今日推荐