DataGrid的多种格式化显示方法

// 1.用<%# DataBinder.Eval(Container.DataItem,"MaritalStatus").ToString()=="1"?"已婚":"未婚" %>方式显示
// 2.用sql中的case Gender when 1 then '男' else '女' end显示
// 3.在后台写方法,前台调用显示,如Format(object item,string type)
// 4.DataGrid1_ItemDataBound

1.html
< HTML >
    
< HEAD >
        
< title > GridFormat </ title >
        
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
    
</ HEAD >
    
< body  MS_POSITIONING ="GridLayout" >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< asp:datagrid  id ="DataGrid1"  style ="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 16px"  runat ="server"
                AutoGenerateColumns
="False"  BorderColor ="#CC9966"  BorderStyle ="None"  BorderWidth ="1px"  BackColor ="White"
                CellPadding
="4" >
                
< FooterStyle  ForeColor ="#330099"  BackColor ="#FFFFCC" ></ FooterStyle >
                
< SelectedItemStyle  Font-Bold ="True"  ForeColor ="#663399"  BackColor ="#FFCC66" ></ SelectedItemStyle >
                
< ItemStyle  ForeColor ="#330099"  BackColor ="White" ></ ItemStyle >
                
< HeaderStyle  Font-Bold ="True"  ForeColor ="#FFFFCC"  BackColor ="#990000" ></ HeaderStyle >
                
< Columns >
                    
< asp:TemplateColumn  HeaderText ="ID" >
                        
< ItemTemplate >
                            
<% # DataBinder.Eval(Container.DataItem,"UserID" %>
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:TemplateColumn  HeaderText ="学位" >
                        
< ItemTemplate >
                            
<% # Format(Container.DataItem,"Degree" %>
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:TemplateColumn  HeaderText ="性别" >
                        
< ItemTemplate >
                            
<% # DataBinder.Eval(Container.DataItem,"Gender" %>
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:TemplateColumn  HeaderText ="婚姻状况" >
                        
< ItemTemplate >
                            
<% # DataBinder.Eval(Container.DataItem,"MaritalStatus").ToString()=="1"?"已婚":"未婚"  %>
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:TemplateColumn  HeaderText ="描述" >
                        
< ItemTemplate >
                            
<% # DataBinder.Eval(Container.DataItem,"Description" %>
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                
</ Columns >
                
< PagerStyle  HorizontalAlign ="Center"  ForeColor ="#330099"  BackColor ="#FFFFCC" ></ PagerStyle >
            
</ asp:datagrid ></ form >
    
</ body >
</ HTML >
2.cs代码
public   class  GridFormat : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
//1.用<%# DataBinder.Eval(Container.DataItem,"MaritalStatus").ToString()=="1"?"已婚":"未婚" %>方式显示
            
//2.用sql中的case Gender when 1 then '男' else '女' end显示
            
//3.在后台写方法,前台调用显示,如Format(object item,string type)
            
//4.DataGrid1_ItemDataBound
            DataBind();
for(int i=0; i < DataGrid1.Items.Count; i++)
   
{   
    
if(DataGrid1.Items[i].Cells[2].Text=="")
    
{
     DataGrid1.Items[i].Cells[
2].ForeColor = System.Drawing.Color.Red;
    }

   }


        }


        
GetDataSet
        
        
DataBind

        
Format        

        
Web Form Designer generated code

      
DataGrid1_ItemDataBound
    }
3.数据库脚本
if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[FormatGrid] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
drop   table   [ dbo ] . [ FormatGrid ]
GO

CREATE   TABLE   [ dbo ] . [ FormatGrid ]  (
    
[ UserID ]   [ int ]   NULL  ,
    
[ Degree ]   [ int ]   NULL  ,
    
[ Gender ]   [ int ]   NULL  ,
    
[ MaritalStatus ]   [ int ]   NULL  ,
    
[ Description ]   [ nvarchar ]  ( 500 ) COLLATE Chinese_PRC_CI_AS  NULL  
ON   [ PRIMARY ]
GO

-- 测试数据
insert   into  FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)  values ( 1 , 1 , 1 , 0 , ' 可以读取流。读取是从流到数据结构(如字节数组)的数据传输。 ' )
insert   into  FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)  values ( 2 , 2 , 0 , 1 , ' 可以写入流。写入是从数据结构到流的数据传输。 ' )
insert   into  FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)  values ( 3 , 3 , 1 , 0 , ' 流可以支持查找。查找是对流内的当前位置进行查询和修改。查找功能取决于流具有的后备存储区类型。例如,网络流没有当前位置的统一概念,因此一般不支持查找。 ' )
insert   into  FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)  values ( 4 , 4 , 0 , 1 , ' 小山哥 ' )
insert   into  FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)  values ( 5 , 5 , 1 , 0 , ' 山哥 ' )
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值