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();
}
}
第四列是用第三列除以第二列作为第四列的值,能不能实现?不能的话应该怎么办?
我要完成第二个图的一个报表的项目,但是遇到问题了,我只做到第一个图那了,用的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();
}
}
第四列是用第三列除以第二列作为第四列的值,能不能实现?不能的话应该怎么办?
帮帮忙,不然做不出来就只能滚蛋了
一般做法
1。先把需要的基础数据找出来
2。针对datatable拆分计算得到多个你想要的数据源(datatable)
3。分别绑定各个repeater控件
其次我们针对上述的第2点做改进
我们分别为我们需要的数据源建立视图,当然所谓列之间的计算就放在创建视图的过程中。总体来说我们有两种处理方法,不同的是一个是在程序的内存中计算,一个是在数据库服务器内存中计算。
又快,行列运算又简单
Table1.ID = "Table1";
Table1.Attributes.Add("width", "100%");
Table1.Attributes.Add("border", "1");
Table1.Attributes.Add("cellpadding", "0");
Table1.Attributes.Add("cellspacing", "0");
Table1.Attributes.Add("bordercolorlight", "#CCCCCC");
Table1.Attributes.Add("bordercolordark", "#FFFFFF");
TableRow r = new TableRow();
r.Height = 32;
TableCell c = new TableCell();
c.Text = "XXX";
c.HorizontalAlign = HorizontalAlign.Center;
c.ForeColor = System.Drawing.Color.Green;
c.Font.Bold = true;
c.BackColor = System.Drawing.Color.FromName("#E0FFE0");
c.Attributes.Add("colspan", "2");
r.Cells.Add(c);
Table1.Rows.Add(r);this.form1.Controls.Add(Table1)如果是我 我就用这种方式 当然GridView也能做
如果用arraylist直接在前台套用html标签就可以了。想要显示数据直接用@XX。你可以上网查一上arraylist.方法。如果用List<model>方法的话。可以在aspx页使用foreach循环。至于显示百分比,或者别的。可以直接在前台去算!具体的去上网查一下。呵呵。看了一下你的代码。建议你使用三层开发。而不要把SQL语句直接写在页面的CS里。看着有些乱。
GOOD LUCK TO YOU!