代碼還用上一節的,把reportFrx的dataSource去掉。各cell綁定的字段也去掉,有了第二節的基礎,現在看這個ms就不難了 無非就是 傳到reportFrx一個數據集 在把這個數據集 綁到各控件里 清空details里的cell的值 各cell 改成數據庫對應列的名字方便綁定。
媽蛋想法是好的 可是我怎么綁都是只顯示一條數據 原來這個項目已經無葯可救
沒辦法新建 了一個項目
1.新建一個項目 叫xtraReportLearn form1上放一個documentView1 再放一個button1 新建 一個空XtraReport 命名為 XReport 在這上邊的details中放幾個XRLabel
XReport代碼如下做用就是 綁定一下數據

namespace XtrReportLearn { public partial class XReport : DevExpress.XtraReports.UI.XtraReport { public XReport() { InitializeComponent(); } //重載一個構造函數 public XReport(DataTable dt ) { InitializeComponent();//這個不可少 this.DataSource = dt; this.UserID.DataBindings.Add("Text", dt, "ID");//綁定 this.UserName.DataBindings.Add("Text", dt, "UserName"); this.Roles.DataBindings.Add("Text", dt, "Roles"); } } }
2.form1代碼如下

namespace XtrReportLearn { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=test")) { using (SqlDataAdapter ada = new SqlDataAdapter("select * from usersTmp", con)) { DataTable dt = new DataTable(); ada.Fill(dt); XReport xr = new XReport(dt); xr.Landscape = false; documentViewer1.DocumentSource = xr; xr.CreateDocument(); } } } } }
3.效果圖
原來的錯誤分析:1.網上說有可能是 報表連接了一些數據集 像sqldatasource query 什么的 綁定的時應全部干掉 我可能干掉的不完全可能
2.我想我控件起名也有問題 xrTable 綁定id的我起名叫ID 可能有沖突(應該不是這個問題 我改成userID后還是只有一行數據)
結論:可能里邊連接數據集什么的的沒刪干凈 最好的方法就是重新建 一個新的report
最后我沒找到原因0.0 如果出這種錯誤就新建一個吧
如果調用打印方法的話還得加一下下邊的引用
using DevExpress.XtraReports.UI;