XtraReport三动态数据绑定

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

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

  XREPORT代码在设计器界面右键显示

复制代码
  public XtraReport1(DataTable dt)
        {
            InitializeComponent();
            this.DataSource = dt;
            //这里面的Text是xrtable的属性 区分大小写
            this.xrTableCell1.DataBindings.Add("Text",dt,"bid");
            this.xrTableCell2.DataBindings.Add("Text", dt, "bname");
            this.xrTableCell3.DataBindings.Add("Text", dt, "shuoming");
            this.xrTableCell4.Text = "编号";
            this.xrTableCell5.Text = "名称";
            this.xrTableCell6.Text = "备注";
            
        }
复制代码

 

Form1代码如下

复制代码
   private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string constr = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1";
            SqlConnection con = new SqlConnection(constr);
            try
            {
                con.Open();
                SqlCommand com = new SqlCommand("select bid,bname,shuoming from book",con);
                SqlDataAdapter dpt = new SqlDataAdapter(com);
                dpt.Fill(dt);
                XtraReport1 report = new XtraReport1(dt);
                report.Landscape = true;
                documentViewer1.DocumentSource = report;
                report.CreateDocument();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
复制代码

理解:

      只有在datalis下面才能显示多行数据,Reportheader只是报表的头部。

  Xrtable  建立在datalis下面,form 下添加控件有splitcontrol 作为容器 dock属性为fill ,documentview 属性dock=fill, navbarcontrol 导航窗体,FORM1属性 windowsstate 设置max最大化

 

posted @   WhiteSpace  阅读(2375)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示