系统为vs2005,数据库sqlserver2000,在数据库中有一盖章字段,类型为bit,我的前台代码为:
<asp:TemplateField HeaderText="盖章">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "Gz")%></ItemTemplate>
<ItemStyle Width="1%" BorderWidth="1px" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
现在数据绑定后显示出来的数据为True或False,我想显示成类似于checkbox的格式,即为True时checkbox打钩,为False时不打钩。前台代码如何改?

解决方案 »

  1.   

    加一个模板列,拖上 checkbox,绑定这一列就行了
      

  2.   


                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:CheckBox ID="chkA" runat="server" 
                                                                Checked='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "state")) %>' 
                                                                AutoPostBack="True" oncheckedchanged="chkA_CheckedChanged" />
                                                        </ItemTemplate>
                                                        <ItemStyle Width="10%" HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    </asp:TemplateField>
      

  3.   

    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Gz")) %>' />
      

  4.   

    to xuexiziji:我在数据库里已经赋值好了0和1,前台直接显示出结果就可以了,不需要 AutoPostBack="True" oncheckedchanged="chkA_CheckedChanged"这个语句,另外你的代码DataBinder.Eval(Container.DataItem, "state")中的"state"是不是我的程序中的"Gz".
      

  5.   

    对你的GridView加一个RowDataBind事件
    当字段显示的是True时,取出此字段值,前用Replace将其转化为CheckBox,并选中(大致是这样转化的:Replace("True", "<asp:CheckBox id='id1' runat='server' check=true"))
    当字段显示的是False时,转化为CheckBox,但未选中
      

  6.   

    checked属性不支持用eval前台绑定
    可以在代码实现:
    <asp:TemplateField>
                  <ItemTemplate>
                    <asp:CheckBox runat="server" ID="CheckBox1"></asp:CheckBox>
                  </ItemTemplate>
    </asp:TemplateField>protected void GridView1_RowDataBound ( object sender, GridViewRowEventArgs e )
        {
            if ( e.Row.RowType == DataControlRowType.DataRow )
            {
                if ( ( ( DataRowView ) e.Row.DataItem ).Row [ "Gz" ].ToString () == "1" )
                {
                    ( ( CheckBox ) e.Row.FindControl ( "CheckBox1" ) ).Checked = true;
                }
                else
                {
                    ( ( CheckBox ) e.Row.FindControl ( "CheckBox1" ) ).Checked = false;
                }
            }
        }
      

  7.   

    问题解决了,是xuexiziji方法,另外checked属性支持用eval前台绑定