A small summary of the use of DataGrid techniques

Source: Internet
Author: User
datagrid| Skills DataGrid Custom Paging navigation


Effect:


Let the DataGrid do this for its own paging
[1] [2] [3] [4] [5] [6]



private void Grid_itemcreated (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (E.item.itemtype==listitemtype.pager)
{
foreach (Control C in E.item.cells[0]. Controls)
{
if (c is Label)//Current page
{
Label lblpage= (label) C;
Lblpage. Forecolor= System.Drawing.ColorTranslator.FromHtml ("#e78a29"); #e78a29, #FF0000
Lblpage. Font.bold=true;
Lblpage. Text= "[<font color= #e78a29 ><b>" +lblpage. Text+ "&LT;/B&GT;&LT;/FONT&GT;]";
((Label) c). ForeColor = System.Drawing.Color.Green;
Break
}
if (c is LinkButton)//links to other pages
{
LinkButton LinkButton = (LinkButton) C;
Linkbutton.text = "[" + linkbutton.text+ "]";
}
}
}
}





Automatic numbering in controls such as DataGrid:

To add a template column:

<asp:templatecolumn headertext= "No." >
<itemstyle horizontalalign= "Center" ></ItemStyle>
<ItemTemplate>
<asp:label runat= "Server" text= ' <%# dgcustomize.currentpageindex*dgcustomize.pagesize+ Dgcustomize.items.count+1%> ' >
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>





Create a complex header in a DataGrid

Method One: Use table to implement

<form id= "Form1" method= "POST" runat= "Server" >
<table id= "Table1" runat= "Server" cellspacing= "1" cellpadding= "2" width= "580" border= "1"
Bgcolor= "#cc6633" "bordercolor=" #cc9966 "style=" FONT-SIZE:9PT; border-bottom:0px ">
&LT;TR align= "center" >
&LT;TD colspan= "2" width= "380" style= "height:21px" ></TD>
&LT;TD width= "colspan=" 2 "style=" height:21px "></TD>
</TR>
&LT;TR align= "center" >
&LT;TD width= "bgcolor=" "#66cc99" ><font face= "song Body" ></FONT></TD>
&LT;TD width= "180" bgcolor= "white" ><font face= "song Body" ></FONT></TD>
&LT;TD width= "160" bgcolor= "#99cccc" ></TD>
&LT;TD width= "bgcolor=" "#009999" ></TD>
</TR>
</TABLE>
<asp:datagrid id= "DataGrid1" width= "580px" alternatingitemstyle-backcolor= "#6699ff" cellpadding= "2"
cellspacing= "1" borderwidth= "1" bordercolor= "#cc9966" font-size= "9pt" runat= "Server" showheader= "False"
autogeneratecolumns= "False" >
<Columns>
<asp:boundcolumn datafield= "Title" >
<itemstyle width= "200px" ></ItemStyle>
</asp:BoundColumn>
<asp:boundcolumn datafield= "CreateDate" >
<itemstyle width= "180px" ></ItemStyle>
</asp:BoundColumn>
<asp:boundcolumn datafield= "pid" >
<itemstyle width= "160px" ></ItemStyle>
</asp:BoundColumn>
<asp:boundcolumn datafield= "Hitcount" >
<itemstyle width= "40px" ></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</form>

Top of form











Bottom of form

Method Two: Dynamically generating a table header

To generate a double table header:
private void Grid_itemdatabound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{

if (E.item.itemtype = = Listitemtype.header)
{
E.item.cells[0]. ColumnSpan = 1;//This is the number of spanned columns in the first column
StringBuilder strtext=new StringBuilder ();
strtext. Append ("\\</td>");
strtext. Append ("<td colspan=4> Life Lighting </td>");
strtext. Append ("<td colspan=2> General lighting </td>");
strtext. Append ("<td colspan=2> industrial </td>");
strtext. Append ("<td colspan=2> Nong



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.