asp.net GridView Implementation schedule display (dynamic merged cells) Implementation steps _ Practical skills

Source: Internet
Author: User
Gridview,asp. NET, I will use this control to implement the display of the curriculum. First of all, what is the difference between the display of the timetable and the display of the normal record? The common records mentioned here refer to the records that are queried directly from the database without any processing. Typically, we display these normal records in the GridView, simply by binding them directly to the GridView. However, the presentation of the timetable is not so simple, it needs to continue processing of ordinary records, according to the specific data in the record to determine which row, which column, and need to be based on the course start time and end time dynamically merged cells, and finally the data display. This is where the timetable shows the difficulty. OK, let's see how I can achieve it.
code in the. aspx file
Copy Code code as follows:

<%@ 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 ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "GridView1" runat= "Server"
Onrowdatabound= "Gridview1_rowdatabound1" borderwidth= "1" >
<rowstyle horizontalalign= "Center" verticalalign= "Middle"/>
</asp:GridView>
</div>
</form>
</body>

the code in the. aspx.cs file
Copy Code code as follows:

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 ();//Create Connection object
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 ()///This table is used to store the converted course table (in the same format as it is seen daily)
Add eight Columns
DTSCHEDULE.COLUMNS.ADD ("Timetable");
for (int i = 1; i < 8; i++)
{
DTSCHEDULE.COLUMNS.ADD ("Week" + Weekconverttochinese (i));
}
Add eight lines
for (int i = 0; i < 8; i++)
{
DTSCHEDULE.ROWS.ADD ();
}
Add fixed left information (in the first few sessions)
for (int i = 0; i < 8; i++)
{
Dtschedule.rows[i][0] = "The first" + Converttochinese (i+1) + "section";
}
This array holds the cell information that needs to be merged. such as: Need to merge the first column of one or two cells
Then, the three numbers of rows in the array are 1,1,2, respectively.
int[][] Temparray = new int[table. Rows.count][];
Array initialization
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;
}
}
Walk through table and fill in the appropriate position in the tab for each timetable information.
for (int i = 0; i < table. Rows.Count; i++)
{
Class is a week's lesson
String week = convert.tostring (table. rows[i]["Week"]);
Class start time
String StartTime =convert.tostring (table. rows[i]["StartTime"]);
Class End Time
String endtime = convert.tostring (table. rows[i]["Endtime"]);
for (int weekcount = 1; weekcount < 8; weekcount++)//Determine which column this piece of data will be displayed in the future
{
if (week = = convert.tostring (dtschedule.columns[weekcount). ColumnName))//compare with week, make sure the data should be written in that column
{
Temparray[i][0] = weekcount;//record week (determine which column the future data is displayed in)
Break
}
}
for (int j = 0; J < DtSchedule.Rows.Count + +) to determine the start and end times of the course and fill in the data
{
String section =convert.tostring (dtschedule.rows[j][0]);/The current line is the first few lessons
if (section = = starttime)//Judge The course start time, determine the location, fill in the data
{
TEMPARRAY[I][1] = j;//to record the start time of the class (determine which row the data data is displayed in)
Dtschedule.rows[j][temparray[i][0]] = convert.tostring (table. rows[i]["Coursename"]) + "<br/>" +
Convert.ToString (table. rows[i]["TeacherName"]);
}
if (section = = Endtime)//Judge course end time, record position
{
TEMPARRAY[I][2] = j;//Recording Course end time
Break
}
}
}
Gridview1.datasource = Dtschedule;
Gridview1.databind ();
Merging cells
for (int i = 0; i < table. Rows.Count; i++)
Groupcol (GridView1, temparray[i][0], temparray[i][1], temparray[i][2]);
}
<summary>
Merge multiple cells in a column
</summary>
<param name= "GridView1" ></param>
<param name= "cols" > the column to be merged </param>
<param name= "Srow" > Start line </param>
<param name= "Erow" > End line </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 = "0"; Break
Case 1:CSTR = "one"; Break
Case 2:CSTR = "two"; Break
Case 3:CSTR = "three"; Break
Case 4:CSTR = "four"; Break
Case 5:cstr = "five"; Break
Case 6:cstr = "six"; Break
Case 7:CSTR = "seven"; Break
Case 8:cstr = "eight"; Break
Case 9:CSTR = "Nine"; Break
}
return (CSTR);
}
Convert Day of the week
string Weekconverttochinese (int x)
{
String CStr = "";
Switch (x)
{
Case 1:CSTR = "one"; Break
Case 2:CSTR = "two"; Break
Case 3:CSTR = "three"; Break
Case 4:CSTR = "four"; Break
Case 5:cstr = "five"; Break
Case 6:cstr = "six"; Break
Case 7:cstr = "Day"; Break
}
return (CSTR);
}
<summary>
So that the contents of the GridView can be wrapped.
</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); Note: All of the HTML code in this column will be exported in HTML format, and if only one of the columns of data needs to be converted, small modifications are required here.
}
}
}
}
}

Final Display effect:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.