http://blog.csdn.net/w87875251l/archive/2010/11/02/5983301.aspx我要完成第二个图的一个报表的项目,但是遇到问题了,我只做到第一个图那了,用的repeater三层嵌套,
private void bind()
{
SqlConnection conn = db.CreateConnection();
string sql = "SELECT Company from gongsia union SELECT Company from gongsib";
DataTable dt = new DataTable();
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
Repeater1.DataSource = dt;
Repeater1.DataBind();
conn.Close();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep2 = (Repeater)e.Item.FindControl("Repeater2");
rep2.ItemDataBound += new RepeaterItemEventHandler(Repeater2_ItemDataBound);
SqlConnection conn = db.CreateConnection(); DataRowView row = (DataRowView)e.Item.DataItem;
string str = row["company"].ToString();
string sql = "select count(computer_idn) as zongshu,company from (select a.*,b.company from computer a,(select b.computer_idn,a.Company,b.IpAddress from gongsia a,BoundAdapter b where left(a.IpAddress,12)=left(b.IpAddress,12) union select b.computer_idn,a.Company,b.IpAddress from gongsib a,BoundAdapter b where a.ipaddress=b.ipaddress ) b where a.computer_idn=b.computer_idn and b.Company='" + str + "') a group by Company order by company";
string sql1 = "select count(computer_idn)as geshu,Company from(select a.Computer_idn,b.ComPany,a.Reason,a.Vulnerability_Idn from CVDetected a,(select a.*,b.company from computer a,(select b.computer_idn,a.Company,b.IpAddress from gongsia a,BoundAdapter b where left(a.IpAddress,12)=left(b.IpAddress,12) union select b.computer_idn,a.Company,b.IpAddress from gongsib a,BoundAdapter b where a.ipaddress=b.ipaddress) b where a.computer_idn=b.computer_idn and b.Company='" + str + "') b where a.computer_idn=b.computer_idn and a.Vulnerability_Idn=2671) a group by Company order by Company";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
rep2.DataSource = ds;
rep2.DataBind();
}
}
// <asp:Label ID="label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"company") %>' ></asp:Label>
//倒数第二层绑定
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep3 = (Repeater)e.Item.FindControl("Repeater3");
DataRowView row = (DataRowView)e.Item.DataItem;
string str =row["company"].ToString(); DataRowView row1 = (DataRowView)e.Item.DataItem;
int a = Convert.ToInt32(row1["zongshu"]);
SqlConnection conn = db.CreateConnection();
string sql = "select count(computer_idn)as geshu,Company from(select a.Computer_idn,b.ComPany,a.Reason,a.Vulnerability_Idn from CVDetected a,(select a.*,b.company from computer a,(select b.computer_idn,a.Company,b.IpAddress from gongsia a,BoundAdapter b where left(a.IpAddress,12)=left(b.IpAddress,12) union select b.computer_idn,a.Company,b.IpAddress from gongsib a,BoundAdapter b where a.ipaddress=b.ipaddress) b where a.computer_idn=b.computer_idn and b.Company='"+str+"') b where a.computer_idn=b.computer_idn and a.Vulnerability_Idn=2671) a group by Company order by Company";
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds,"a");
rep3.DataSource = ds.Tables["a"];
rep3.DataBind();
conn.Close();
}
}
第四列是用第三列除以第二列作为第四列的值,能不能实现?不能的话应该怎么办?
private void bind()
{
SqlConnection conn = db.CreateConnection();
string sql = "SELECT Company from gongsia union SELECT Company from gongsib";
DataTable dt = new DataTable();
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
Repeater1.DataSource = dt;
Repeater1.DataBind();
conn.Close();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep2 = (Repeater)e.Item.FindControl("Repeater2");
rep2.ItemDataBound += new RepeaterItemEventHandler(Repeater2_ItemDataBound);
SqlConnection conn = db.CreateConnection(); DataRowView row = (DataRowView)e.Item.DataItem;
string str = row["company"].ToString();
string sql = "select count(computer_idn) as zongshu,company from (select a.*,b.company from computer a,(select b.computer_idn,a.Company,b.IpAddress from gongsia a,BoundAdapter b where left(a.IpAddress,12)=left(b.IpAddress,12) union select b.computer_idn,a.Company,b.IpAddress from gongsib a,BoundAdapter b where a.ipaddress=b.ipaddress ) b where a.computer_idn=b.computer_idn and b.Company='" + str + "') a group by Company order by company";
string sql1 = "select count(computer_idn)as geshu,Company from(select a.Computer_idn,b.ComPany,a.Reason,a.Vulnerability_Idn from CVDetected a,(select a.*,b.company from computer a,(select b.computer_idn,a.Company,b.IpAddress from gongsia a,BoundAdapter b where left(a.IpAddress,12)=left(b.IpAddress,12) union select b.computer_idn,a.Company,b.IpAddress from gongsib a,BoundAdapter b where a.ipaddress=b.ipaddress) b where a.computer_idn=b.computer_idn and b.Company='" + str + "') b where a.computer_idn=b.computer_idn and a.Vulnerability_Idn=2671) a group by Company order by Company";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
rep2.DataSource = ds;
rep2.DataBind();
}
}
// <asp:Label ID="label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"company") %>' ></asp:Label>
//倒数第二层绑定
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep3 = (Repeater)e.Item.FindControl("Repeater3");
DataRowView row = (DataRowView)e.Item.DataItem;
string str =row["company"].ToString(); DataRowView row1 = (DataRowView)e.Item.DataItem;
int a = Convert.ToInt32(row1["zongshu"]);
SqlConnection conn = db.CreateConnection();
string sql = "select count(computer_idn)as geshu,Company from(select a.Computer_idn,b.ComPany,a.Reason,a.Vulnerability_Idn from CVDetected a,(select a.*,b.company from computer a,(select b.computer_idn,a.Company,b.IpAddress from gongsia a,BoundAdapter b where left(a.IpAddress,12)=left(b.IpAddress,12) union select b.computer_idn,a.Company,b.IpAddress from gongsib a,BoundAdapter b where a.ipaddress=b.ipaddress) b where a.computer_idn=b.computer_idn and b.Company='"+str+"') b where a.computer_idn=b.computer_idn and a.Vulnerability_Idn=2671) a group by Company order by Company";
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds,"a");
rep3.DataSource = ds.Tables["a"];
rep3.DataBind();
conn.Close();
}
}
第四列是用第三列除以第二列作为第四列的值,能不能实现?不能的话应该怎么办?
1.用DataGrid显示数据
2.数据先在数据库中作好统计,DataGrid只是负责显示
3.在DataGrid的OnItemDataBound事件中设置表头显示(思路,先clear掉表头,然后添加一列,再将表头的HTML写到改列的text即可)
或者你也可以找第三方控件,如:DevExpress,不过一般收费,D的不好找,只能自己破
http://blog.csdn.net/w87875251l/archive/2010/11/02/5983301.aspx
或者
在SQL语句进行计算定义一个别名然后绑定这个别名就行
复杂的报表为什么不使用水晶报表或者Report Service呢然后嵌到web页面呢
private DataTable bind()
{
SqlConnection conn = db.CreateConnection();
string sql = "SELECT Company from gongsia union SELECT Company from gongsib";
DataTable dt = new DataTable();
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
Repeater1.DataSource = dt;
Repeater1.DataBind();
return dt;
conn.Close();
}
1、可以通过sql语句(或者存储过程)得到一个DataTable
2、或者重新组建一个DataTable
不然的话 在嵌套的 控件的rowdatabound 或 itemdatabound 事件里
获取对应行列的值 计算结果 再赋值 给对应行列
或者用xslt去生成
代码也简单。