GridView,ASP.NET中很常用的數(shù)據(jù)顯示控件,這里,我將用這個(gè)控件來實(shí)現(xiàn)課程表的顯示。首先說說課程表的顯示與普通記錄的顯示有何不同?這里所說的普通記錄是指直接從數(shù)據(jù)庫中查詢出來的、沒有經(jīng)過任何處理的記錄。通常,我們用GridView顯示這些普通記錄,只需直接將這些記錄表綁定到GridView中即可。但是,課程表的顯示可不是這么簡單,它需要將普通記錄繼續(xù)加工,需要根據(jù)記錄中具體的數(shù)據(jù)來確定數(shù)據(jù)需要顯示在哪一行、哪一列,而且需要根據(jù)課程開始時(shí)間和結(jié)束時(shí)間動(dòng)態(tài)合并單元格,最后才是數(shù)據(jù)的顯示。這就是課程表顯示的難點(diǎn)之所在。好了,下面就看看我是如何實(shí)現(xiàn)的吧。
.aspx文件中代碼:
復(fù)制代碼 代碼如下:
%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="DataBind.test" %>
%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>/title>
/head>
body>
form id="form1" runat="server">
div>
asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound1" BorderWidth="1">
HeaderStyle Wrap="False" />
RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
/asp:GridView>
/div>
/form>
/body>
/html>
.aspx.cs文件中代碼:
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
namespace DataBind
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = DB.createCon();//創(chuàng)建連接對(duì)象
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("Select * from Schedule ", con);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable table = new DataTable();
table = ds.Tables[0];
DataTable dtSchedule = new DataTable();//此表用于存放轉(zhuǎn)換后的課程表(格式與日常見到的一樣)
//添加八列
dtSchedule.Columns.Add("課程表");
for (int i = 1; i 8; i++)
{
dtSchedule.Columns.Add("星期" + WeekConvertToChinese(i));
}
//添加八行
for (int i = 0; i 8; i++)
{
dtSchedule.Rows.Add();
}
//添加左側(cè)固定信息(第幾節(jié)課)
for (int i = 0; i 8; i++)
{
dtSchedule.Rows[i][0] = "第" + ConvertToChinese(i+1) + "節(jié)";
}
//此數(shù)組用于存放需要合并的單元格信息。如:需要合并第一列的一、二單元格
//那么,數(shù)組中一行的三個(gè)數(shù)分別為1,1,2
int[][] tempArray = new int[table.Rows.Count][];
//數(shù)組初始化
for (int i = 0; i table.Rows.Count; i++)
{
tempArray[i] = new int[3];
for (int j = 0; j 3; j++)
{
tempArray[i][j] = 0;
}
}
//遍歷table,將每條課表信息填在tab中適當(dāng)?shù)奈恢谩?
for (int i = 0; i table.Rows.Count; i++)
{
//課是周幾的課
string week = Convert.ToString(table.Rows[i]["Week"]);
//課開始時(shí)間
string startTime =Convert.ToString( table.Rows[i]["StartTime"]);
//課結(jié)束時(shí)間
string endTime = Convert.ToString(table.Rows[i]["EndTime"]);
for (int weekCount = 1; weekCount 8; weekCount++)//確定本條數(shù)據(jù)將來顯示在哪一列
{
if (week == Convert.ToString(dtSchedule.Columns[weekCount].ColumnName))//跟星期做比較,確定數(shù)據(jù)應(yīng)該寫在那一列
{
tempArray[i][0] = weekCount;//記錄星期(確定將來的數(shù)據(jù)顯示在哪一列)
break;
}
}
for (int j = 0; j dtSchedule.Rows.Count; j++)//確定課程的開始時(shí)間和結(jié)束時(shí)間,并填寫數(shù)據(jù)
{
string section =Convert.ToString( dtSchedule.Rows[j][0]);//當(dāng)前行是第幾節(jié)課
if (section == startTime)//判斷課程開始時(shí)間,確定位置,填寫數(shù)據(jù)
{
tempArray[i][1] = j;//記錄上課開始時(shí)間(確定數(shù)據(jù)數(shù)據(jù)顯示在哪一行)
dtSchedule.Rows[j][tempArray[i][0]] = Convert.ToString(table.Rows[i]["CourseName"]) + "br />" +
Convert.ToString(table.Rows[i]["TeacherName"]);
}
if (section == endTime)//判斷課程結(jié)束時(shí)間,記錄位置
{
tempArray[i][2] = j;//記錄課結(jié)束時(shí)間
break;
}
}
}
GridView1.DataSource = dtSchedule;
GridView1.DataBind();
//合并單元格
for (int i = 0; i table.Rows.Count; i++)
GroupCol(GridView1, tempArray[i][0], tempArray[i][1], tempArray[i][2]);
}
/// summary>
/// 合并某列中的多個(gè)單元格
/// /summary>
/// param name="GridView1">/param>
/// param name="cols">要合并的那一列/param>
/// param name="sRow">開始行/param>
/// param name="eRow">結(jié)束行/param>
public static void GroupCol(GridView GridView1, int cols, int sRow, int eRow)
{
//if (GridView1.Rows.Count 1 || cols > GridView1.Columns.Count - 1)
//{
// return;
//}
//if (GridView1.Rows.Count 1 || cols > GridView1.Rows[0].Cells.Count - 1)
//{
// return;
//}
TableCell oldTc = GridView1.Rows[sRow].Cells[cols];
for (int i = 1; i = eRow - sRow; i++)
{
TableCell tc = GridView1.Rows[sRow + i].Cells[cols];
tc.Visible = false;
if (oldTc.RowSpan == 0)
{
oldTc.RowSpan = 1;
}
oldTc.RowSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
}
string ConvertToChinese(int x)
{
string cstr = "";
switch (x)
{
case 0: cstr = "零"; break;
case 1: cstr = "一"; break;
case 2: cstr = "二"; break;
case 3: cstr = "三"; break;
case 4: cstr = "四"; break;
case 5: cstr = "五"; break;
case 6: cstr = "六"; break;
case 7: cstr = "七"; break;
case 8: cstr = "八"; break;
case 9: cstr = "九"; break;
}
return (cstr);
}
//轉(zhuǎn)換星期幾
string WeekConvertToChinese(int x)
{
string cstr = "";
switch (x)
{
case 1: cstr = "一"; break;
case 2: cstr = "二"; break;
case 3: cstr = "三"; break;
case 4: cstr = "四"; break;
case 5: cstr = "五"; break;
case 6: cstr = "六"; break;
case 7: cstr = "日"; break;
}
return (cstr);
}
/// summary>
/// 使得GridView中的內(nèi)容可以換行
/// /summary>
/// param name="sender">/param>
/// param name="e">/param>
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCellCollection cells = e.Row.Cells;
foreach (TableCell cell in cells)
{
cell.Text = Server.HtmlDecode(cell.Text); //注意:此處所有的列所有的html代碼都會(huì)按照html格式輸出,如果只需要其中的哪一列的數(shù)據(jù)需要轉(zhuǎn)換,此處需要小的修改即可。
}
}
}
}
}
最終顯示效果:
您可能感興趣的文章:- datalist,Repeater和Gridview的區(qū)別分析
- asp.net中讓Repeater和GridView支持DataPager分頁
- repeater、gridview 在綁定時(shí)判斷判斷顯示不同的行樣式或文本
- ASP.NET MVC4之js css文件合并功能(3)
- Asp.net程序優(yōu)化js、css實(shí)現(xiàn)合并與壓縮的方法
- Asp.net_Table控件の單元格縱向合并示例
- asp.net中GridView和DataGrid相同列合并實(shí)現(xiàn)代碼
- asp.net中rdlc 合并行的方法
- asp.net 合并GridView中某列相同信息的行(單元格)
- ASP.NET中GridView和Repeater重復(fù)數(shù)據(jù)如何合并