如何在GridView中绑定chekbox? [英] How to bind chekbox in gridview?

查看:71
本文介绍了如何在GridView中绑定chekbox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好..
我是asp.net的新手..我正在制作水晶报表,其中我必须在gridview中绑定chekbox..im无法进行编码..我以.aspx形式返回了以下代码.检查的property.plz出现错误,请帮帮我.

Helo..
I m new to asp.net..I m making crystal reports,in which i have to bind chekbox in gridview..i m not able to do coding..I have return the following code in my .aspx form.bt its throwing me error for checked property.plz help me.

<asp:GridView ID="GridView1" runat="server"

            AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="StaffName">
                <ItemTemplate>

                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("StaffName") %>' /></ItemTemplate>
                    <HeaderStyle CssClass="grdheadermster" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

推荐答案


检查此
Hi ,
Check this
protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          GridView1.DataSource = GetData();
          GridView1.DataBind();
      }

  }
  DataTable GetData()
  {
      DataTable dt = new DataTable();
      using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
      {
          con.Open();
          using (SqlCommand cmd = new SqlCommand("select productid ,flag from product ", con))
          {

              SqlDataAdapter adpt = new SqlDataAdapter(cmd);
              adpt.Fill(dt);
          }

      }
      return dt;
  }


<div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField HeaderText="id">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text="<%# Bind('id') %>"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Check">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked="<%# Bind('flag') %>" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

</div>



最好的问候
M.Mitwalli



Best Regards
M.Mitwalli


hiii,
如果您的职员名是字符串,那么再做一件事

hiii,
if your staffname is string then do one more thing

<asp:templatefield>
  <itemtemplate>
  <asp:checkbox id="chkOnlineCheck" runat="server" checked="<%# Eval("staffname").ToString() == "sample" ? false : true) %>" />' />
  </itemtemplate>
</asp:templatefield>




在哪里用自己的方式替换以下条件

Eval("staffname").ToString()=="sample"




where replace follwing condition by your own

Eval("staffname").ToString() == "sample"


这篇关于如何在GridView中绑定chekbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆