代碼還用上一節的,把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最大化