Simple implementation of a crosstab 2: Using a front-end program to implement

Source: Internet
Author: User
Tags crosstab
The previous article introduces the simple implementation of a crosstab 1: Using stored procedures, which are taken in front of the program implementation.
Implementation essentials:
1. Read all target scores (flatscroretable)
2. Extract test time (not duplicates) from the target score as a list header for the cross table
3. Extract the test subjects from the target score (not duplicates) as the row header of the crosstab table
4. A DataTable (Crossscroretable) is built dynamically based on 2, which has an account column, a number of exam time columns, and other information.
5. Write the test account in 3 to the DataTable
6. Read score value from flatscroretable fill in crossscroretable corresponding cell
Description: Easy to understand, here the crosstab crossscroretable imagined as an Excel table, the column name as the list header, the account name as the row header.

Main code:
void Loadscoredata ()
{
Read all the grades of the specified student #region read all the results of the specified student
string stuname = Drpstu.selectedvalue;
String connstr = "server=.;D Atabase=demolib; Uid=sa ";
String sql = "Select Subjectname, Studentname, Scorevalue, examdate from t_score WHERE studentname = @StuName";
DataSet ds = new DataSet ();
SqlDataAdapter da = new SqlDataAdapter (SQL, CONNSTR);
Da.SelectCommand.CommandType = CommandType.Text;
Da. SELECTCOMMAND.PARAMETERS.ADD ("@StuName", stuname);
Da. Fill (ds, "Flatscore");
#endregion Read all the results of a specified student

DataTable flatscoretable = ds. Tables[0];
DataView Flatscoreview = new DataView (flatscoretable);
Read Account list, excluding duplicates
DataTable subjectlist;
. NET 2 Using the ToTable method supported by DataView
Subjectlist = flatscoreview.totable ("Subjectlist", True, "subjectname");
Subjectlist = selectdistinct (flatscoretable, "Subjectlist", True, "subjectname");
Read the test time list, excluding duplicates
. NET 2
DataTable examdatelist;
. NET 2 Using the ToTable method supported by DataView
Examdatelist = flatscoreview.totable ("Examdatelist", True, "examdate");
Examdatelist = selectdistinct (flatscoretable, "Examdatelist", True, "examdate");

Create a crosstab #region create a crosstab table
DataTable crossscoretable = new DataTable ();

Constructing table Schema #region table schema
Account columns
CROSSSCORETABLE.COLUMNS.ADD ("Subject", typeof (String));
Test Time column, with time as column name
foreach (DataRow r in Examdatelist.rows) {
CROSSSCORETABLE.COLUMNS.ADD ((DateTime) r[0]). ToString ("yyyy m-month DD Day"), typeof (String));
}
#endregion Construct Table Schema

Read and write data from the score table read and write data from the #region
DataColumnCollection cols = crossscoretable.columns;
By first account (row) after time (column) traversal
foreach (DataRow r in Subjectlist.rows) {
DataRow NewRow = Crossscoretable.newrow ();
Subjects
Newrow[0] = r[0];
Score, traversal time
for (int i=1; i< cols. Count; i++) {
Returns the results of a specified account and a specified test
eg. "Subjectname= ' Sun Guang ' and examdate= ' 2007-5-19 '"
                flatscoreview.rowfilter =<

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.