Automatic Filling of the rows of the GridView Author: Meng xianhui published at: 22:40:59
In some development practices, sometimes you need to fill the GridView into a table with a fixed number of rows for the beautiful page. The following method implements this filling function. This method can automatically generate columns and template columns. Note: Due to the time relationship, completely blank data is not filled. Haha, if necessary, you can add it yourself :)!
C #
& Lt; % @ Page Language = "C #" % & gt;
& Lt ;! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd & gt;
& Lt; script runat = "server" & gt;
// Calculate the data, which can be obtained from the data
ICollection createperformancebyxianhuimeng ()
{
System. Data. DataTable dt = new System. Data. DataTable ();
System. Data. DataRow dr;
Dt. Columns. Add (new System. Data. DataColumn ("Student Class", typeof (System. String )));
Dt. Columns. Add (new System. Data. DataColumn ("Student name", typeof (System. String )));
Dt. Columns. Add (new System. Data. DataColumn ("", typeof (System. Decimal )));
Dt. Columns. Add (new System. Data. DataColumn ("Mathematics", typeof (System. Decimal )));
Dt. Columns. Add (new System. Data. DataColumn ("English", typeof (System. Decimal )));
Dt. Columns. Add (new System. Data. DataColumn ("computer", typeof (System. Decimal )));
For (int I = 0; I & lt; 8; I ++)
{
System. Random rd = new System. Random (Environment. TickCount * I );;
Dr = dt. NewRow ();
Dr [0] = "class" + I. ToString ();
Dr [1] = "Chapter E of Mencius" + I. ToString ();
Dr [2] = System. Math. Round (rd. NextDouble () * 100, 2 );
Dr [3] = System. Math. Round (rd. NextDouble () * 100, 2 );
Dr [4] = System. Math. Round (rd. NextDouble () * 100, 2 );
Dr [5] = System. Math. Round (rd. NextDouble () * 100, 2 );
Dt. Rows. Add (dr );
}
System. Data. DataView dv = new System. Data. DataView (dt );
Return dv;
}
// Set the number of lines displayed on each page
Int totalrowcount = 12;
// Number of automatically filled rows
Int numcount = 0;
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Gridview1.datasource = createperformancebyxianhuimeng ();
Gridview1.databind ();
}
}
Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
// Calculate the number of automatically filled rows
Numcount ++;
}
If (E. Row. rowtype = datacontrolrowtype. footer)
{
// After calculation, add the missing row here
Int toleft = totalrowcount-numcount;
Int numcols = gridview1.rows [0]. cells. count;
For (INT I = 0; I & lt; toleft; I ++)
{
Gridviewrow ROW = new gridviewrow (-1,-1, datacontrolrowtype. emptydatarow, datacontrolrowstate. Normal );
For (Int J = 0; J & lt; numcols; j ++)
{
Tablecell cell = new tablecell ();
Cell. Text = "& amp; nbsp ;";
Row. cells. Add (cell );
}
Gridview1.controls [0]. Controls. addat (numcount + 1 + I, row );
}
}
}
& Lt;/script & gt;
& Lt; html xmlns = "http://www.w3.org/1999/xhtml" & gt;
& Lt; head runat = "server" & gt;
& Lt; Title & gt; automatically fill in the gridview with a fixed number of rows & lt;/Title & gt;
& Lt;/Head & gt;
& Lt; Body & gt;
& Lt; Form ID = "form1" runat = "server" & gt;
& Lt; ASP: gridview id = "gridview1" runat = "server" onrowdatabound = "gridview1_rowdatabound"
Font-size = "12px" cellpadding = "3" & gt;
& Lt; headerstyle backcolor = "# ededed"/& gt;
& Lt; columns & gt;
& Lt; ASP: templatefield headertext = "template column" & gt;
& Lt; itemtemplate & gt;
& Lt; % # eval ("Student name") % & gt;
& Lt;/itemtemplate & gt;
& Lt;/ASP: templatefield & gt;
& Lt;/columns & gt;
& Lt;/ASP: gridview & gt;
& Lt;/Form & gt;
& Lt;/Body & gt;
& Lt;/html & gt;
Address: http://dotnet.saike.org/article/ee911c00-0e99-4e50-a941-3defa599ef48/read.aspx