在做報表時,通常都是以DataTable的方式,
與rdlc的定義DataSet作對應
但若查詢出的資料~還要繼續做處理,必須要操作Sql敘述或者操作DataTable物件
例如:
dt.rows[0]["name"] ...etc.
操作過程很不直覺~
若可以與資料Mapping的物件,感覺就很直觀
此篇文章就利用物件集合來與rdlc作繫結,進而產生報表
1.首先建立報表需要的DataSet
並選擇要使用的對應資料表tcustomer
2.建立報表並加入資料集
並配製Report後
設定好ReportView對應Report檔後
3.就可針對資料的繫結實作
code:
//對應tcusotmer的自訂物件
public class Ctcustomer
{
private System.Int32 _fUserId;
public System.Int32 fUserId
{
get
{
return _fUserId;
}
set
{
_fUserId = value;
}
}
private System.String _fName;
public System.String fName
{
get
{
return _fName;
}
set
{
_fName = value;
}
}
private System.String _fPhone;
public System.String fPhone
{
get
{
return _fPhone;
}
set
{
_fPhone = value;
}
}
}
//按下查詢的程式
try
{
ReportViewer1.Visible = true;
List <Ctcustomer> customerList = new List<Ctcustomer>();
string sql = "Select * from tcustomer where fName like'" + txtKeys .Text + "%' ";
//SqlHelp為筆者的連線物件,在此也可自行做ADO撈資料
CSqlHelp d = new CSqlHelp();
DataTable dt= d.SelectAdapter(sql);
//輪巡資料來源加入給物件集合
foreach (DataRow row in dt.Rows)
{
Ctcustomer enty = new Ctcustomer();
enty.fName = row["fName"].ToString ();
enty.fPhone = row["fPhone"].ToString();
customerList.Add(enty);
}
//取得list後,可利用Linq to Object 繼續作邏輯判斷及資料處理
//將資料丟給xsd的DataTable ; 那邊是rdlc自行定義的DbTestDataSet_customer空殼
ReportDataSource rds = new ReportDataSource("DbTestDataSet_customer", customerList);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
}
catch (Exception ex)
{
//處理Exception
}
Result:
以上提供參考...
頁首:
日期 > =First(Fields!ReportDate.Value, "DataSet1")