A commonly used data display control in gridview and ASP. NET. Here, I will use this control to display the curriculum. First, what is the difference between the display of the curriculum and the display of common records? The general record here refers to the record that is directly queried from the database and has not been processed. Normally, you only need to bind these record tables to the gridview to display these normal records using the gridview. However, the display of the curriculum is not that simple. It needs to process general records and determine the row and column of data to be displayed based on the specific data in the records, in addition, you need to dynamically merge cells based on the course start time and end time, and finally display the data. This is the difficulty of the Curriculum display. Well, let's see how I implement it.
Code in the. aspx file:
<%@ 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">
Code in the. aspx. CS file:
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 the connection object sqldataadapter SD A = 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 curriculum (the format is the same as that seen in daily life) // Add eight columns of dtschedule. columns. add ("course schedule"); For (INT I = 1; I <8; I ++) {dtschedule. columns. add ("Week" + weekconverttochinese (I);} // Add eight rows for (in T I = 0; I <8; I ++) {dtschedule. rows. add () ;}// Add the fixed information on the left side (Lesson number) for (INT I = 0; I <8; I ++) {dtschedule. rows [I] [0] = "+" + converttochinese (I + 1) + "";} // This array is used to store the information of cells to be merged. For example, to merge the first and second cells in the first column, the three numbers of rows in the array are respectively, 2 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 ;}// traverse the table and fill in the information of each course table in the appropriate position on the tab. For (INT I = 0; I <table. rows. count; I ++) {// the number of weeks. String week = convert. tostring (table. rows [I] ["Week"]); // course start time string starttime = convert. tostring (table. rows [I] ["starttime"]); // course end time string endtime = convert. tostring (table. rows [I] ["endtime"]); For (INT weekcount = 1; weekcount <8; weekcount ++) // determine the columns in which the data will be displayed in the future {If (week = convert. tostring (dtschedule. columns [weekcount]. columnname) // compare with the week, Confirm that the data should be written in that column {temparray [I] [0] = weekcount; // record the week (determine which column of future data is displayed) break ;}} for (Int J = 0; j <dtschedule. rows. count; j ++) // determine the course start time and End Time, and enter the data {string section = convert. tostring (dtschedule. rows [J] [0]); // the number of courses in the current row if (Section = starttime) // you can determine the course start time and position, enter the data {temparray [I] [1] = J; // record the class start time (determine the row where the data is displayed) dtschedule. rows [J] [temparray [I] [0] = convert. tostring (table. rows [I] ["coursename"]) + "<br /> "+ Convert. tostring (table. rows [I] ["teachername"]);} If (Section = endtime) // you can specify the end time of the course and specify the position {temparray [I] [2] = J; // record course end time break ;}}gridview1.datasource = dtschedule; gridview1.databind (); // merge 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"> </PAR Am> /// <Param name = "Cols"> the column to be merged </param> /// <Param name = "srow"> Start row </param> /// <Param name = "erow"> end row </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 = gridv Iew1.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 = "1"; break; Case 2: CSTR = "2"; break; Case 3: CSTR = "3"; break; Case 4: CSTR = "4"; break; Case 5: CSTR = "5"; break; Case 6: CSTR = "6"; break; Case 7: CSTR = "7"; break; case 8: CSTR = "8"; break; Case 9: CSTR = "9 "; break;} return (CSTR);} // string weekconverttochinese (int x) {string CSTR = ""; Switch (x) {Case 1: CSTR = "1"; break; Case 2: CSTR = "2"; break; Case 3: CSTR = "3"; break; Case 4: CSTR = "4 "; break; Case 5: CSTR = "5"; break; Case 6: CSTR = "6"; break; Case 7: CSTR = ""; break;} return (CSTR );} /// <summary> /// enables the content in the gridview to wrap. /// </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 CE Ll in cells) {Cell. TEXT = server. htmldecode (cell. text); // Note: All the HTML code of all columns here will be output in HTML format. If you only need to convert the data of which column, A small modification is required here. }}}}}
Final display effect: