Reporting简单应用
时间:2010-10-18 来源:pinky
1.创建需要的Web项目ReportDemo
2.创建类库,提供数据源List,生成解决方案
(1).添加类UserInfo.cs
public class UserInfo
{
public string Id { set; get; }
public string Name { set; get; }
public string Age { set; get; }
}
(2).提供数据UserOper.cs
public class UserOper
{
public List<UserInfo> GetList()
{
List<UserInfo> lstUserInfo = new List<UserInfo>();
UserInfo user = new UserInfo();
user.Id = "3001";
user.Name = "刘若英";
user.Age = "20";
lstUserInfo.Add(user);
user = new UserInfo();
user.Id = "3002";
user.Name = "刘德华";
user.Age = "24";
lstUserInfo.Add(user);
user = new UserInfo();
user.Id = "3003";
user.Name = "刘小喜";
user.Age = "28";
lstUserInfo.Add(user);
return lstUserInfo;
}
public DataTable GetDataTable()
{
DataTable dTable = new DataTable();
dTable.Columns.Add("编号");
dTable.Columns.Add("姓名");
dTable.Columns.Add("性别");
dTable.Columns.Add("年龄");
DataRow dRow = null;
dRow = dTable.NewRow();
dRow["编号"] = "1001";
dRow["姓名"] = "张三";
dRow["性别"] = "女";
dRow["年龄"] = "23";
dTable.Rows.Add(dRow);
dRow = dTable.NewRow();
dRow["编号"] = "1002";
dRow["姓名"] = "李四";
dRow["性别"] = "男";
dRow["年龄"] = "25";
dTable.Rows.Add(dRow);
dRow = dTable.NewRow();
dRow["编号"] = "1003";
dRow["姓名"] = "王二";
dRow["性别"] = "男";
dRow["年龄"] = "28";
dTable.Rows.Add(dRow);
return dTable;
}
}
3.Web项目中添加报表ReportUser.rdlc,
报表数据-> 新建->数据集(直接选择创建的类库) ->
命名数据集名称: dsUser1/ dsUser2-> 可用数据集选择要绑定的集合
(此示例演示报表可绑定List泛型集合和DataTable数据) -> 完成
4.报表工具箱,选择要显示的报表项拖到页面
->若要绑定数据直接拖报表数据中绑定的数据字段就可
5.web页面编写绑定代码
.aspx:
<rsweb:ReportViewer ID="rvList" runat="server" Width="100%" Height="100%">
</rsweb:ReportViewer>
.cs :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
initialize();
}
}
private void initialize()
{
UserOper userOper = new UserOper();
rvList.LocalReport.DataSources.Clear();
rvList.LocalReport.ReportPath = @"ReportUser.rdlc";
//DataSet1与创建报表的数据源一致,报表数据源对象的类型必须为
//System.Data.DataTable、System.Collections.IEnumerable
//或 System.Web.UI.IDataSource。
ReportDataSource rdt1 = new ReportDataSource("dsUser1", userOper.GetList());
ReportDataSource rdt2 = new ReportDataSource("dsUser2", userOper.GetDataTable());
rvList.LocalReport.DataSources.Add(rdt1);
rvList.LocalReport.DataSources.Add(rdt2);
rvList.DataBind();
}