new 一个DataTable,把两表的中数据合并,放到datatable中。
string sql = "select * from MZ_FPXM"; SqlDataReader sdr = null; SqlDataReader sdr2 = null; DataTable dt = new DataTable(); DataRow dr = null; using (sdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sql, null)) { dt.Columns.Add("科室", typeof(System.String)); while (sdr.Read()) { dt.Columns.Add(new DataColumn(sdr["MC"].ToString(), typeof(System.Decimal))); } dt.Columns.Add(new DataColumn("发票张数", typeof(System.String))); dt.Columns.Add(new DataColumn("人次", typeof(System.String))); dt.Columns.Add(new DataColumn("合计", typeof(System.Decimal))); sum = new double[dt.Columns.Count]; SqlParameter[] parms = new SqlParameter[]{ new SqlParameter("@YYID",yybh), new SqlParameter("@KSBH","ALL"), new SqlParameter("@RQFR",ksrq), new SqlParameter("@RQTO",jsrq) }; using (sdr2 = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "PROC_MZKSGZL", parms)) { DataView dv = dt.DefaultView; dv.Sort = "科室"; while (sdr2.Read()) { int rowIndex = dv.Find(sdr2["KS"].ToString()); if (rowIndex == -1) { dr = dt.NewRow(); dr["科室"] = sdr2["KS"].ToString(); if (sdr2["SFLB"].ToString() == "西药") dr[sdr2["SFLB"].ToString() + "费"] = Math.Round(decimal.Parse(sdr2["JE"].ToString()), 2); else dr[sdr2["SFLB"].ToString()] = Math.Round(decimal.Parse(sdr2["JE"].ToString()), 2); dr["发票张数"] = Math.Round(decimal.Parse(sdr2["SJZS"].ToString()), 2); dr["人次"] = Math.Round(decimal.Parse(sdr2["RC"].ToString()), 2); dr["合计"] = Math.Round(decimal.Parse(sdr2["ZJJE"].ToString()), 2); dt.Rows.Add(dr); } else { if (sdr2["SFLB"].ToString() == "西药") dt.Rows[rowIndex][sdr2["SFLB"].ToString() + "费"] = Math.Round(decimal.Parse(sdr2["JE"].ToString()), 2); else dr[sdr2["SFLB"].ToString()] = Math.Round(decimal.Parse(sdr2["JE"].ToString()), 2); } } } } gvInfo.DataSource = dt; gvInfo.DataBind();