在使用DataGrid 的时候,总是会有各种各样的需求,在和数据库打交道的项目中用的最多的恐怕就属DataGrid 了吧,微软有时候做的就差那么一点点,所以就需要我们自己来动手实现啦.

DataGrid 自定义分页导航

无需任何其他第三方控件,在DataGrid 自己分页的基础上再个性化一点.
效果:  

让DataGrid自己的分页实现这样的效果
[1][2][3][4][5][6] 
  

让DataGrid自己的分页实现这样的效果
[1][2][3][4][5][6]

 

DataGrid等控件中的自动编号:

添加一个模版列:

<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>

DataGrid中创建复杂表头

方法一:用table实现

  <form ></ItemStyle>
     </asp:BoundColumn>
    </Columns>
   </asp:DataGrid>   
  </form> 

窗体顶端

 

 

 

 

 

 

 

 

 

 

 

 

窗体底端

方法二:动态生成表头

生成双层表头:
  private void grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {

   if (e.Item.ItemType == ListItemType.Header)
   {
//    e.Item.Cells[0].ColumnSpan = 1;//
这是第一列的跨列数
    StringBuilder strtext=new StringBuilder();
    strtext.Append("\\</td>");
    strtext.Append("<td colspan=4>
生活照明</td>");
    strtext.Append("<td colspan=2>
一般照明</td>");
    strtext.Append("<td colspan=2>
工付业</td>");
    strtext.Append("<td colspan=2>
农业</td>");
    strtext.Append("<td colspan=2>
合计</td>");
    strtext.Append("</tr>");
    strtext.Append("<tr>");
    strtext.Append("<td>" + e.Item.Cells[0].Text);               
    e.Item.Cells[0].Text =strtext.ToString();

   }

}

整个表头内容:<tr><td>  e.Item.Cells[0].Text =的内容  </td></tr>

加起来就是表头的样式。

生活照明

 

 

一般照明

 

 

工付业

 

 

农业

 

 

合计

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

生成三层表头:

if (e.Item.ItemType == ListItemType.Header)
   {    
    StringBuilder strtext=new StringBuilder();
    strtext.Append("</td>");
    strtext.Append("<td colspan= 6>
当月</td>");
    strtext.Append("</tr>"); 
   strtext.Append("<tr>"); 
    strtext.Append("<td colspan=2>
居民</td>");
    strtext.Append("<td colspan=2>
一般</td>");
    strtext.Append("<td colspan=2>
工付业</td>");
    strtext.Append("</tr>"); 
    strtext.Append("<tr>");
    strtext.Append("<td>" + e.Item.Cells[0].Text);               
    e.Item.Cells[0].Text =strtext.ToString();
   } 
 

当月

 

 

居民

 

 

一般

 

 

工付业

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关文章:

  • 2022-12-23
  • 2022-03-03
  • 2022-12-23
  • 2021-06-15
  • 2021-10-02
  • 2022-01-01
  • 2022-01-06
  • 2022-02-23
猜你喜欢
  • 2021-10-12
相关资源
相似解决方案