zoukankan      html  css  js  c++  java
  • 更改DataGrid里数据绑定的值

    用DataGrid做数据绑定的时候,如显示性别等,数据库里放的是对应的0或1。在DataGrid里我们一般需要显示的是男或女,而不是0或1,如何实现呢?当然,在数据库查询的时候就可以直接使用case去做,那么如果查询出来是0/1这种情况,怎么在DataGrid把这个数字转换成男/女显示呢?
    在aspx页面里对应的是这样的:
                        <asp:BoundColumn DataField="sex" HeaderText="sex" Visible="False">
                            
    <ItemStyle Width="100px"></ItemStyle>
                        
    </asp:BoundColumn>
                        
    <asp:TemplateColumn HeaderText="性别" ItemStyle-Width="100">
                            
    <ItemTemplate>
                                
    <asp:Label id="lblSex" Runat="server"></asp:Label>
                            
    </ItemTemplate>
                        
    </asp:TemplateColumn>

    在DataGrid的ItemDataBound方法里这样写:
            private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            
    {
                Label label
    =(Label)(e.Item.FindControl("lblSex"));
                
    if(e.Item.Cells[0].Text=="0")
                
    {
                    label.Text
    ="";
                }

                
    if(e.Item.Cells[0].Text=="1")
                
    {
                    label.Text
    ="";
                }

            }
  • 相关阅读:
    基础知识---抽象类和接口
    基础知识---数组和链表
    基础知识---枚举
    基础知识---IEnumerable、ICollection、IList、IQueryable
    [翻译]微软 Build 2019 正式宣布 .NET 5
    基础知识---const、readonly、static
    简说设计模式
    Java修行之路
    简说设计模式——迭代器模式
    简说设计模式——备忘录模式
  • 原文地址:https://www.cnblogs.com/songafeng/p/124815.html
Copyright © 2011-2022 走看看