但是问题来了,不同科系的同学的科目是不一样的,那么我们在数据库设计的时候通常是把学生、某科成绩作为一条记录,那么这个时候我们就需要做一个行转列的逻辑处理了。
解决方法:
使用GridView来生成表单,这个实现起来会比较麻烦,如果要在列表里面显示链接就更不可能了;
生成html再输出到页面中,这个实现起来比较灵活、方便;
基本功能点:
动态生成表头;
数据进行分页;
查询数据;
对每个成绩进行超链接,查看明细;
页面代码
复制代码代码如下:
<div id="dataDiv1">
<asp:Literal ID="labtable" runat="server"></asp:Literal>
<div class="toolBlock" style="border-top:solid 1px #C0CEDF; padding-top:5px">
<bw:VirtualPager ID="VirtualPager1" runat="server" ItemsPerPage="10" OnPageIndexChanged="VirtualPager1_PageIndexChanged" />
</div>
</div>
后台代码(生成html字符串)
复制代码代码如下:
/// <summary>
/// table方式加载考核成绩.
/// Step1:通过年月来获取唯一的批次.
/// Step2:生成表头.
/// Step3:开始循环每个用户.
/// Step4:循环每个用户的考核项.
/// </summary>
protected void BindTable()
{
labtable.Text = string.Empty;
//通过年月来获取唯一的批次.
string year = DropDownImportYear.SelectedValue;
string month = DropDownImportMonth.SelectedValue;
string group_KH_Date = year + month;
GroupService groupService = new GroupService();
DataTable dtGroup = groupService.GetByTemplateCodeAndGroup_KH_Date(TemplateCode, group_KH_Date);
if (dtGroup.Rows.Count > 0)
{
Guid groupID = (Guid)DbUtils.ToGuid(dtGroup.Rows[0]["ID"]);
int groupStutas = DbUtils.ToInt32(dtGroup.Rows[0]["GroupStutas"].ToString());
DataTable dtCells = personalGradeService.GetDistinctTemplateItemNameByGroupID(groupID);
GroupID = groupID.ToString();
if (dtCells.Rows.Count > 0)
{
int cellsCount = 0;//考核项数.
StringBuilder strtable = new StringBuilder();
strtable.AppendFormat("<table cellpadding=\"0\" cellspacing=\"0\" class=\"tblClass\">");
strtable.AppendFormat("<tr>");
strtable.AppendFormat("<th>用户名</th>< /span>");
cellsCount = dtCells.Rows.Count;
for (int i = 0; i < cellsCount; i++)
{
strtable.AppendFormat("<th>{0}</th>", dtCells.Rows[i]["Name"].ToString());
}
strtable.AppendFormat("</tr>");
DataTable dtGradeData = GetData(cellsCount);
int cursor = 1;//表示一个用户的第一条记录.
string userName = string.Empty;
string realName = string.Empty;
for (int i = 0; i<dtGradeData.Rows.Count; i++)
{
if (cursor == 1)
{
userName = dtGradeData.Rows[i]["UserName"].ToString();
realName = dtGradeData.Rows[i]["RealName"].ToString();
strtable.AppendFormat("<td width=\"12%\">{0}</td>", realName);
}
string id = dtGradeData.Rows[i]["ID"].ToString();
string templateItemCode = dtGradeData.Rows[i]["TemplateItemCode"].ToString();
strtable.AppendFormat("<td width=\"50px\">");
string GradeData = dtGradeData.Rows[i]["GradeData"].ToString();
if (!string.IsNullOrEmpty(GradeData) && GradeData != "0")
{
GradeData=Math.Round(decimal.Parse(GradeData), 2).ToString();
//转换成两位小数
}
strtable.AppendFormat("{0}", GradeData);
//未发布的成绩才能修改
if (groupStutas != (int)Botwave.MRPS.Constants.GroupStutas.FinishPush)
{
strtable.AppendFormat("<br /><a style=\"text-decoration:underline\" class=\"thickbox\" href='UserGradeDataEdit.aspx?ID={0}&BeModifer={1}&KeepThis=true&TB_iframe=true&height=250&width=500'>", id, userName);
strtable.AppendFormat("<img src=\"../res/gradeedit.gif\" align=\"baseline\" />
几个辅助方法(如何处理好分页问题,这就关系到cellsCount参数的使用了,cellsCount表示科目数)
复制代码代码如下:
protected void btnSearch_Click(object sender, EventArgs e)
{
SearchNow = true;
VirtualPager1.CurrentPageIndex = 1;
BindTable();
}
protected void VirtualPager1_PageIndexChanged(object sender, Botwave.Web.Controls.PageChangedEventArgs e)
{
BindTable();
}
/// <summary>
/// 获取考核成绩.
/// </summary>
/// <returns></returns>
private DataTable GetData(int cellsCount)
{
int nRecordCount = 0;
string condition = "";
string filterName = txtUserName.Text.Trim();//姓名搜索.
StringBuilder sb = new StringBuilder();
sb.AppendFormat(" GroupID ='{0}'", GroupID);
if (!string.IsNullOrEmpty(filterName))
sb.AppendFormat(" AND (UserName like '%{0}%' OR RealName like '%{0}%')", filterName);
condition = sb.ToString();
if (SearchNow)//当在很多页的时候,或者是最后一页,这个时候来点击查询,就会报错了CurrentPageIndex,这个做法就是为了防止这个.
VirtualPager1.CurrentPageIndex = 0;
DataTable dt = personalGradeService.GetGradeData(VirtualPager1.CurrentPageIndex, VirtualPager1.ItemsPerPage * cellsCount, condition, ref nRecordCount);
VirtualPager1.TotalRecordCount = nRecordCount / cellsCount;
VirtualPager1.DataBind();
SearchNow = false;//要重新复制.
return dt;
}
业务逻辑(分页)
复制代码代码如下:
public System.Data.DataTable GetGradeData(int currentPageIndex, int pageSize, string condition, ref int nRecordCount)
{
string fieldShow = " UserID,TemplateCode,TemplateItemCode,GradeData,ID,UserName,RealName ";
string fieldOrder = " UserName ASC,TemplateItemCode ASC ";
string where = condition;
if (String.IsNullOrEmpty(where))
{
where = "1=1";
}
return IBatisDbHelper.GetPagedList("dbo.vw_UserGradeData", "ID", currentPageIndex, pageSize, fieldShow, fieldOrder, where, ref nRecordCount);
}
注意事项:
在使用<asp:Literal ID="labtable" runat="server"></asp:Literal>来在页面输出html来显示表单的话,要注意字符串的长度,如果html太长的话,就会出错了,因为Literal的有8000个字符的限制的;
在数据库中对数据进行好排序,这样才能保证数据跟表头是一一对应的;
这里有个前提,那就是查询出来的数据都是包含同等多的记录的,比如同学A和同学B的科目是一样多的;因为不一样多,那这个动态表单就没什么意义了。如果真的有不同,也是可以做出来的,但是会麻烦一点;