首页 > 其他分享 >GridView中CheckBox的数据绑定显示选中和未选中问题

GridView中CheckBox的数据绑定显示选中和未选中问题

时间:2023-04-28 17:35:34浏览次数:37  
标签:GridView Checked cbMemberPrice1 绑定 CheckBox 选中 Row

https://www.cnblogs.com/zxd543/p/3121169.html

效果如下(以会员价为例)

会员价(MemberPrice)字段的数据库类型为int(1表示true,0表示false)

页面绑定如下:

复制代码
<asp:TemplateField HeaderText="会员价">
    <ItemStyle HorizontalAlign="Center" Width="60px" />
        <ItemTemplate>
            <asp:CheckBox ID="cbMemberPrice1" runat="server" 
                  Checked='<%# Convert.ToBoolean(Eval("MemberPrice")) %>' />
    </ItemTemplate>
</asp:TemplateField>
复制代码

绑定也可换成:Checked = ‘<%#(Eval("MemberPrice", "{0}") == "1") ? true:false%>’或

                   Checked = ‘<%#Eval("MemberPrice").ToString()=="1"?true:false%>‘  

                    MemberPrice数据库字段

也可以在后台绑定(推荐)

页面需要绑定,不加判断。操作麻烦,但是便于更改和维护GridView的RowDataBound事件

e.Row.RowType == DataControlRowType.DataRow //判断是否为数据行否则读取标题行会报错

复制代码
 Text='<%# Eval("MemberPrice")%>' />

protected void gvShow_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox cbMemberPrice1 = (CheckBox)e.Row.FindControl("cbMemberPrice1");
        cbMemberPrice1.Checked = cbMemberPrice1.Text == "1" ? true : false;
        cbMemberPrice1.Text = string.Empty;
    }
}
复制代码

完成效果开头图像所示:成功捆绑CheckBox状态

标签:GridView,Checked,cbMemberPrice1,绑定,CheckBox,选中,Row
From: https://www.cnblogs.com/Dongmy/p/17362747.html

相关文章

  • JS获取table中选中某几行其中某一列数值的总和
    JS获取table中选中某几行其中某一列数值的总和一、思路1.如何获取某几行,并且可以实时变化数值?实现如下:$("input[type='checkbox']").click(function(){alert($(this).val());})2.接下来就是实现当每次触发点击事件以后,然后,计算其中的值,实现如下:<script>$(functi......
  • 修改radio单选按钮的“圆点”选中颜色
     1、css样式/*单选换颜色*/.radio{position:relative;display:inline-block;font-weight:400;}.radioinput{position:absolute;left:-9999px;background-color:#ffffff;}.radioi{displa......
  • import treeTransfer from "el-tree-transfer"; 全量树去除 选中的
    <template><div><tree-transfer:title="['源列表','目标列表']":from_data="fromData":to_data="toData":defaultProps="{label:'label'}"@add......
  • java js JavaScript 设置html:radio的默认选中, js也可以用el表达式
    <html:radioproperty="consumptionClass"value="花了">花了</html:radio><html:radioproperty="consumptionClass"value="赚了">赚了</html:radio><html:radioproperty="consumptionClass"va......
  • GridView 同行item高度不一致问题
    GridView同行item高度不一致问题//bug场景:item高度不一致,存在留白间隙 解决办法:将GridView添加到它本身的适配器当中,新增ViewHolder(目的是在GridView初始化完成后,适配器方便操作GridView,直接在适配器getView方法中对converView进行操作),计算GridView高度,并设置GridView......
  • 关于el-checkbox-group 报错 length 未定义
     问题原因:页面在初始化的时候el-checkbox-group的v-model要绑定一个数组,但是我的在生命周期的created中才为其绑定数据,造成再渲染dom时绑定数据类型不对而报错解决办法:1.在视图渲染之前给该值赋值为空数组 解决办法:2在data中定义的数据初始化一个数组推荐使用方法2,方法1......
  • EAS_在ListUIETCX.java中校验是否选中行
    /***对内背书*/publicvoidactionEndorseIn_actionPerformed(ActionEvente)/**/throwsException/**/{checkSelected();ArrayListidList=getSelectedIdValues();ReceivableBillCollec......
  • 使用Vue实现点击事件变颜色并且显示选中文字
    首先需要引入Vue.js!!!!!   直接上代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><scriptsrc="../static/vue.js"></script><st......
  • EAS_字啊listUICTEX中获取选中的id
    @OverridepublicvoidactionInTransfer_actionPerformed(ActionEvente)/**/throwsException/**/{ArrayListidList=getSelectedIdValues();SelectorItemCollectionsic=newSelectorItemCollection();......
  • 20230425001 - DataGridView绑定了数据之后, 再添加CheckBox列的解决方案
                 DataGridViewCheckBoxColumncheckBoxColumn=newDataGridViewCheckBoxColumn();           checkBoxColumn.Name="select";           checkBoxColumn.HeaderText="选择";           dgv_M.Columns.Inse......