您的位置:首页 > 其它

取特定行列,进行行转列操作,制作报表

2013-08-13 13:48 176 查看
当我们在制作报表的时候,往往需要根据实际情况来获取数据,当该数据没有出现 ,我们就不显示。同时我们又需要将查询到的某个表中的某列的值来作为 该表的列名,由于用到的数据库是sql server2000的 而非sql server2005 ,所以 2005中的pivot函数我们就无法使用。

制作效果如图



文中,需要用的表范例:

1.异常情况表对应

//异常情况
string strSqlYcqk = string.Concat(new object[] { "select distinct(c.ycxxcontent)  from t_brycxx a, t_ycxxcode b, t_ycxxcontent c, t_brxx d, t_yhxx e where a.ycxx_id = c.ycxx_id and b.yclxcode = c.yclxcode and a.blh = d.blh and a.jlhs_id = e.id and a.jlsj > '", time, "' and a.jlsj < '", time2, "' group by c.ycxxcontent ;" });




2.护士姓名表对应:

//护士姓名
string strSqlHsxm = string.Concat(new object[] { "select distinct(e.yhxm)  from t_brycxx a, t_ycxxcode b, t_ycxxcontent c, t_brxx d, t_yhxx e where a.ycxx_id = c.ycxx_id and b.yclxcode = c.yclxcode and a.blh = d.blh and a.jlhs_id = e.id and a.jlsj > '", time, "' and a.jlsj < '", time2, "' group by e.yhxm ;" });



3.护士人次表对应:

string strSql = string.Concat(new object[] { "select distinct(c.ycxxcontent) as 异常情况 ,e.yhxm as 护士姓名 ,count(*) as 护士人次   from t_brycxx a, t_ycxxcode b, t_ycxxcontent c, t_brxx d, t_yhxx e where a.ycxx_id = c.ycxx_id and b.yclxcode = c.yclxcode and a.blh = d.blh and a.jlhs_id = e.id and a.jlsj > '", time, "' and a.jlsj < '", time2, "' group by e.yhxm ,c.ycxxcontent ;" });



一.从数据库中获取数据,保存到表中:

DateTime time = this.dtpBegin.Value;
DateTime time2 = this.dtpEnd.Value.Date.AddDays(1.0);
string strSql = string.Concat(new object[] { "select distinct(c.ycxxcontent) as 异常情况 ,e.yhxm as 护士姓名 ,count(*) as 护士人次   from t_brycxx a, t_ycxxcode b, t_ycxxcontent c, t_brxx d, t_yhxx e where a.ycxx_id = c.ycxx_id and b.yclxcode = c.yclxcode and a.blh = d.blh and a.jlhs_id = e.id and a.jlsj > '", time, "' and a.jlsj < '", time2, "' group by e.yhxm ,c.ycxxcontent ;" });
//异常情况
string strSqlYcqk = string.Concat(new object[] { "select distinct(c.ycxxcontent)  from t_brycxx a, t_ycxxcode b, t_ycxxcontent c, t_brxx d, t_yhxx e where a.ycxx_id = c.ycxx_id and b.yclxcode = c.yclxcode and a.blh = d.blh and a.jlhs_id = e.id and a.jlsj > '", time, "' and a.jlsj < '", time2, "' group by c.ycxxcontent ;" });
//护士姓名
string strSqlHsxm = string.Concat(new object[] { "select distinct(e.yhxm)  from t_brycxx a, t_ycxxcode b, t_ycxxcontent c, t_brxx d, t_yhxx e where a.ycxx_id = c.ycxx_id and b.yclxcode = c.yclxcode and a.blh = d.blh and a.jlhs_id = e.id and a.jlsj > '", time, "' and a.jlsj < '", time2, "' group by e.yhxm ;" });


上述字符串是sql语句,具体如何绑定到datatable中不再赘述。只是罗列相关代码:
DataSet ds = new DataSet();
try
{
ds = this._wsController.Getdataset("eoiis", strSql + strSqlYcqk+strSqlHsxm);  //数据绑定
DataTable dt = ds.Tables[0];      //获取第一个查询得到的数据的表,该表为异常情况,护士姓名,以及护士人次表
DataTable dtYcqk = ds.Tables[1];  //获取第二个查询得到的数据的表,该表为所出现的异常情况表
DataTable dtHsxm = ds.Tables[2];  //获取第三个查询得到的数据的表,该表为所出现的护士姓名的表
DataTable dt_res = new DataTable();  //绑定datagridview的表


   接下来操作的是将异常情况表中的第一列(总共只有一列)的值 作为datagridview 也就是表dt_res的列名:
//列名
dt_res.Columns.Add("护士");//第一列为护士
foreach (DataRow dr in dtYcqk.Rows)
{
string col_name = dr[0].ToString();
dt_res.Columns.Add(col_name);
}


 然后将护士姓名作为表的行名:
//行
foreach (DataRow dr in dtHsxm.Rows)
{
string row_name = dr[0].ToString();
dt_res.Rows.Add(row_name);
}


 接下来要做的就是统计次数:
   首先,循环列名所在的表,再获取行名所在的表,额 表述的不太恰当,总之先循环行,得到第一行异常情况名称,嵌套循环取到第一行护士名称,然后再通过嵌套循环取到对应的值:

//统计次数
for (int i = 0; i < dtYcqk.Rows.Count; i++)//循环异常情况表
{
for (int j = 0; j < dtHsxm.Rows.Count; j++)//循环护士名称表
{
string count="-";
foreach (DataRow dr in dt.Rows)//循环护士人次表,
{
if (dtYcqk.Rows[i][0].ToString() == dr[0].ToString() && dtHsxm.Rows[j][0].ToString() == dr[1].ToString())//在当取到某行某列的位置与护士人次表所对应的位置相同时,赋值
{
count = dr[2].ToString();
break;
}
}
dt_res.Rows[j][i + 1] = count;//将值放到对应的位置
}
}

dgvYcxxHSsl.DataSource = dt_res;//绑定表到datagridview中


合计:合计可加在上面制作行名和列名的循环后面

dt_res.Columns.Add("合计");
dt_res.Rows.Add("合计");


//合计
for (int i = 0; i < dt_res.Rows.Count; i++)
{
int sum = 0;
for (int j = 1; j < dt_res.Columns.Count-1; j++)
{
if (dt_res.Rows[i][j].ToString() != "-" && dt_res.Rows[i][j].ToString() != "")
{
sum +=int.Parse(dt_res.Rows[i][j].ToString());
}
}
dt_res.Rows[i][dt_res.Columns.Count - 1] = sum;
}
for (int i = 1; i < dt_res.Columns.Count; i++)
{
int sum = 0;
for (int j = 0; j < dt_res.Rows.Count; j++)
{
if (dt_res.Rows[j][i].ToString() != "-" && dt_res.Rows[j][i].ToString() != "")
{
sum += int.Parse(dt_res.Rows[j][i].ToString());
}
}
dt_res.Rows[dt_res.Rows.Count - 1][i] = sum;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: