. The code that generates a two-dimensional table in net

Source: Internet
Author: User
It took a long time to find it.   NET to generate two-dimensional form of code, do not dare to enjoy, now posted out for everyone to see, I believe it helps everyone.

The code is as follows:

Copy Code code as follows:




void Page_Load (Object o, EventArgs e)


... {


DataTable dt = GetData (); Assume GetData returns the DataTable


//probably better to use Hashtable for depts and months too, but to keep the order, let's use ArrayList


string sseparator = ":";


ArrayList aldept = new ArrayList (); Kinds of


ArrayList almonth = new ArrayList (); Code


Hashtable ht = new Hashtable ();


foreach (DataRow dr in Dt. Rows)


{


string sdept = dr["C"]. ToString ();


string sMonth2 = dr["P"]. ToString ();


//Convert the origin code to the origin name


string smonth = GetData2 (sMonth2);


if (!aldept.contains (sdept))


Aldept.add (sdept);


if (!almonth.contains (Smonth))


Almonth.add (Smonth);


ht[sdept+ sseparator + smonth] = dr["a"];


}


TableRow tr = new TableRow ();


TableCell tc = new TableCell ();


//TC. Text = "";


//tr. Cells.add (TC);


foreach (String sdept in aldept)


{


int i=0; Used to calculate the number of a species


foreach (String smonth in Almonth)


{


if (ht[sdept+ sseparator + smonth]==null)


{


i=i+0;


}


Else


{


i = i + int. Parse (ht[sdept+ sseparator + smonth). ToString ());


}


}


TC = new TableCell ();


TC. text= sdept+ "(" +i+ ")";


tr. Cells.add (TC);


}


/**//*foreach (String sdept in aldept)


{


TC = new TableCell ();


Tc. Text= sdept;


tr. Cells.add (TC);


} */


TABLE1.ROWS.ADD (TR);


foreach (String smonth in Almonth)


{


tr = new TableRow ();


/**//*TC = new TableCell ();


TC. Text = Smonth;


tr. Cells.add (TC); */


foreach (String sdept in aldept)


{


TC = new TableCell ();


if (ht[sdept+ sseparator + smonth]==null)


{


TC. Text=smonth+ "(0)";


}


Else


{


TC. Text = smonth+ "(" + ht[sdept+ Sseparator + smonth]. ToString () + ")";


}


tr. Cells.add (TC);


}


TABLE1.ROWS.ADD (TR);


}


}


the code generated by the Web Forms Designer #region The code generated by the Web Forms Designer


override protected void OnInit (EventArgs e)


{


//


//CodeGen: This call is required for the ASP.net Web forms Designer.


//


InitializeComponent ();


base. OnInit (e);


}


/**//// 


///Designer supports the desired method-do not modify
with the Code Editor

///The contents of this method.


/// 


private void InitializeComponent ()


{


this. Load + = new System.EventHandler (this. Page_Load);


}


#endregion


public DataTable GetData ()


{


STATISTICSB stat=new STATISTICSB ();


DataSet Dataset=stat.bystone ();


return DataSet. tables["Stat"];


}


//Get Name list


public string GetData2 (string statid)


{


STATISTICSB stat=new STATISTICSB ();


return Stat.changetoname (Statid);


}

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.