Under normal circumstances, the DataGrid is a record in the corresponding data source! If now my source data table has only 3 fields, can I make my DataGrid display 5 corresponding records in a row? The answer is yes! Let's take a look at the specific implementation method.
Assume that there are three columns in the source table, UserNM, UserId, Note. The final DataGrid is shown in 5 columns.
1. We must conversion to the source table, and a line of the new table after the conversion includes 5 source tables record:
Private DataTable Makedt (DataTable DT, IC ICOLS)
DT is the source table to be converted, ICOLS is a new line of the new table contains the number of records of the source table. This example is 5
The column name of the new table obtained after the conversion begins with COL0, to col (Icols * iColOfsrc-1)
{
INT iColofsrc = 3; number of columns to be converted
INT II, JJ, KK, LL;
INT INEWROWS;
String strcolnm = ""
DataTable newdt = new dataatable ();
For (ii = 0; II { Strcolnm = "col" II.toString (); Newdt.columns.add (new Datacolumn (StrColnm, TypeOf (String))); } IF (dt.rows.count == 0) Return newdt; inewrows = (dt.rows.count iCOLS-1) / icols; KK = 0; For (ii = 0; II { DataRow DR = Newdt.newrow (); For (JJ = 0; JJ { KK = II * ICOLS JJ; IF (KK { LL = JJ * ICOLOFSRC; Strcolnm = "col" ll.tostring (); DR [strcolnm] = dt.rows [kk] ["usernm"]; LL = JJ * ICOLOFSRC 1; Strcolnm = "col" ll.tostring (); DR [strcolnm] = dt.rows [kk] ["userid"]; LL = JJ * ICOLOFSRC 2; Strcolnm = "col" ll.tostring (); DR [strcolnm] = dt.rows [kk] ["Note"]; } Else { LL = JJ * ICOLOFSRC; Strcolnm = "col" ll.tostring (); DR [strcolnm] = "" LL = JJ * ICOLOFSRC 1; Strcolnm = "col" ll.tostring (); DR [strcolnm] = "" LL = JJ * ICOLOFSRC 2; Strcolnm = "col" ll.tostring (); DR [strcolnm] = "" } } Newdt.Rows.Add (DR); } Return newdt; } 2, in the DataGrid's bind method, each cell can be set to White after DataBind: Source Data Table is stored in ViewState ["DS_USERINFO"], whose viewstate is DataSet Void BindDataGrid () { IF (ViewState ["DS_USERINFO"]! = NULL) { DataSet DS = New Dataset (); DS = (Dataset) ViewState ["DS_USERINFO"]; DataTable DT = New DataTable (); DT = Makedt (DS.Tables ["Tab_user"], 5); note the parameter 5 here! This.dg_user.dataSource = DT.DEFAULTVIEW; THIS.DG_USER.DATABIND (); The following code sets all cells to White INT II, JJ; For (ii = 0; II { IF ((this.dg_user.items [ii] .ItemType == ListItemType.Item) || (this.dg_user.items [ii] .ItemType == ListItemType.alternatingItem) || (this.dg_user.items [ii] .ItemType == ListItemType.SelectedItem)) { For (JJ = 0; JJ <5; JJ ) { THIS.DG_USER.Items [ii] .cells [jj] .backcolor = system.drawing.color.white; } } } } } 3, in the ItemCommand event processing: Private void DG_User_ItemCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E) { INT iClick = -1; IF (e.commandname.compareto ("col0") == 0) Iclick = 0; IF (e.commandname.compareto ("col3") == 0) Iclick = 1; IF (e.commandname.compareto ("col6") == 0) Iclick = 2; IF (e.commandname.compareto ("col9") == 0) Iclick = 3; IF (e.commandname.compareto ("col12") == 0) Iclick = 4; IF (iClick == -1) Return; this.dg_user.selectedIndex = E.Item.itemindex; INT II, JJ; For (II = 0; II { IF ((this.dg_user.items [ii] .ItemType == ListItemType.Item) || (this.dg_user.items [ii] .ItemType == ListItemType.alternatingItem) || (this.dg_user.items [ii] .ItemType == ListItemType.selectedItem)) { For (JJ = 0; JJ <5; JJ ) { THIS.DG_USER.Items [ii] .cells [jj] .backcolor = system.drawing.color.white; } } } E.Item.cells [iClick * 3] .BackColor = system.drawing.color.mediumspringgreen; } 4. In the ASPX file, the first list of DataGrid is bound to the button column of the col0, followed by colly COL0, COL1, COL2; push, bind other column data in this class.