Click here to Skip to main content
15,883,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..

C#
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...!!!
Posted

1 solution

Hi Yatin,

Re-write your code as below.
C#
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.
 
Share this answer
 
v2
Comments
Yatin chauhan 22-Nov-12 1:28am    
It Select All the check-box. actually i have open one model popup box and inside that i bind grid view.
here is that :-
<asp:GridView ID="GrdRightDetails" runat="server" CssClass="mGrid"
AutoGenerateColumns="false" GridLines="Both" CaptionAlign="Top" CellPadding="2"
CellSpacing="2" onrowdatabound="GrdRightDetails_RowDataBound">
<columns>
<asp:TemplateField HeaderText="PageNo">
<itemtemplate>
<asp:Label ID="lblPageID" runat="server" Text='<%#Eval("ID") %>' >


<asp:TemplateField HeaderText="PageName">
<itemtemplate>
<asp:Label ID="lbldisplayname" runat="server" Text='<%#Eval("DisplayName") %>' >


<asp:TemplateField HeaderText="Status">
<itemtemplate>
<asp:CheckBox ID="ChkStatus" runat="server" />



Mohd. Mukhtar 22-Nov-12 1:38am    
This is because you are using same user id for each row. Which you are taking form hidden field, Put this hidden field as well into gridview and then check.
Yatin chauhan 22-Nov-12 1:42am    
What value store i in that hiddenfiled which is inside my popup control??
Mohd. Mukhtar 22-Nov-12 1:46am    
It should be user ID but the hidden field should be in gridview. And when you bind the gridview each rows data will have associated userid.
Yatin chauhan 22-Nov-12 2:06am    
its already in gridview.
here is my full aspx page code:-

<asp:GridView ID="GrdUser" runat="server" DataKeyNames="UserID" CssClass="Grid" AutoGenerateColumns="false">
<columns>
<asp:BoundField DataField="EmployeeName" HeaderText="User Name" />
<asp:TemplateField HeaderText="Rights">
<itemtemplate>
<asp:ImageButton ID="imgbtn" ImageUrl="~/Images/icon2.jpg" runat="server" Width="15" Height="15" onclick="imgbtn_Click" ToolTip="Click To Show Rights" />



<PagerStyle CssClass="GridPager" />
<alternatingrowstyle cssclass="GridAltItem">
<PagerSettings PageButtonCount="5" />
<rowstyle cssclass="GridItem">
<HeaderStyle CssClass="GridHeader" />



<!--Packing Details-->
<asp:Button ID="btnShowPopup" runat="server" style="display:none" />
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlpopup" CancelControlID="btnCancel" BackgroundCssClass="modalBackground">
<animations>
<onshown>
<fadein duration=".75" fps="20">




<asp:Panel ID="pnlpopup" runat="server" BackColor="White" Height="50%" Width="90%">
<table style="border:Solid 3px #888" cellpadding="0" cellspacing="0">
<tr style="background-color:#D55500">
<td>
<asp:HiddenField ID="HiddenField1" runat="server" Value="0" />
</td>
</tr>
<tr>
<td align="center">

-: Rights Details :-


</td>
</tr>
<tr>
<td>
<asp:GridView ID="GrdRightDetails" runat="server" CssClass="mGrid"
AutoGenerateColumns="false" GridLines="Both" CaptionAlign="Top" CellPadding="2" CellSpacing="2" onrowdatabound="GrdRightDetails_RowDataBound">
<columns>
<asp:TemplateField HeaderText="PageNo">
<itemtemplate>
<asp:Label ID="lblPageID" runat="server" Text='<%#Eval("ID") %>' >


<asp:Templa

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900