Recently a feature was made, and the DataGrid was grouped according to a column when it was displaying specific content.
After each grouping specific content, add a line to display the total information.
When querying data, union All is used to combine grouped data with detail data, using a sort that achieves the desired effect.
When binding the data, in order to make the total row more eye-catching, so the merge row is bold, the total column is centered.
Interface such as:
DataGrid Front Desk:
1<asp:datagrid id="dglist"Datakeyfield="ID"runat="Server"autogeneratecolumns="False"Onitemdatabound="Dglist_itemdatabound">2<Columns>3<asp:templatecolumn headertext="Column 1">4<ItemTemplate>5<asp:label id="Lab_cop_g_no"runat="Server"text='<% #Eval ("Cop_g_no")%>'></asp:Label>6</ItemTemplate>7</asp:TemplateColumn>
DataGrid Background ItemDataBound Method:
1 protected voidDglist_itemdatabound (Objectsender, DataGridItemEventArgs e)2 {3 if(E.item.itemtype = = ListItemType.Item | | e.item.itemtype = =ListItemType.AlternatingItem)4 {5Label Lab_cop_g_no = (label) E.item.findcontrol ("Lab_cop_g_no");6 if(Lab_cop_g_no. Text = ="Total:")7 {8E.item.font.bold =true;9 Ten((TableCell) lab_cop_g_no. Parent). HorizontalAlign =Horizontalalign.center; One } A } -}
Judging by the contents of column 1, is "total:", row bold display E.item.font.bold = true;
The total is shown in the show ((TableCell) lab_cop_g_no. Parent). HorizontalAlign = Horizontalalign.center;
Recommended E.item.findcontrol This way of writing, the foreground uses template columns, so that the foreground adjustment column order does not affect the use of the background.
C # DataGrid Sets line font bold cell alignment based on the contents of a column