根据选择查询在GridView中检查复选框 [英] Checked chekbox inside gridview based on select query

查看:66
本文介绍了根据选择查询在GridView中检查复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,所有朋友,

我想根据选择查询选中复选框.我的复选框在gridview.i内,但尝试不多,但效果不理想.这是代码..

Hello All Friends,

i want to checked checkbox based on select query . my checkbox is inside gridview.i try to little but it not work perfectly. here is code..

protected void GrdRightDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.CheckBox chk = (System.Web.UI.WebControls.CheckBox)e.Row.FindControl("ChkStatus");

            SqlConnection con = new SqlConnection(ConnString);
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            DataRowView drv = (DataRowView)e.Row.DataItem;
            con.Open();
            string QryCount = "SELECT * FROM RightMaster WHERE UserID='" + Convert.ToInt32(HiddenField1.Value) + "' AND Status = 1";
            da = new SqlDataAdapter(QryCount, con);
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                chk.Checked = true;
                chk.Checked = Convert.ToBoolean(drv["Status"]);
            }
        }
    }



任何人都可以告诉我如何解决这个问题或其他想法吗?
感谢所有人... !!!



Can any one tell me how can i solve this or any other idea..
Thank To All...!!!

推荐答案

Yatin,

如下所示重新编写代码.
Hi Yatin,

Re-write your code as below.
protected void GrdRightDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      System.Web.UI.WebControls.CheckBox chk = (System.Web.UI.WebControls.CheckBox)e.Row.FindControl("ChkStatus"); 
      string QryCount = "SELECT * FROM RightMaster WHERE UserID='" + Convert.ToInt32(HiddenField1.Value) + "' AND Status = 1";
      try
      {
         SqlConnection con = new SqlConnection(ConnString);
         SqlCommand cmd = new SqlCommand(con, QryCount);       
         DataSet ds = new DataSet();
         con.Open();
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         da.Fill(ds);
         if (ds.Table[0].Rows.Count>0)
         {
            chk.Checked = true;
            //chk.Checked = Convert.ToBoolean(drv["Status"]); this line not required if you want to update checked box according to your select query
         }
      }
      Catch {}
    }
}


如果您遇到任何问题,请通知我.
希望对您有帮助.


If you face any issue please let me know.
Hope this will help you.


这篇关于根据选择查询在GridView中检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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