Binding grid view on page load with check box checked as per conditions Asp.net,C#

Hi friends  we have a table named log and  its having columns id,name,status so on page load we want to show the grid view of this table data and those who having status 2 as selected check box.
so now we can check the code section.
create a grid view like this on your aspx page, and include  onrowdatabound="grdLevels_RowDataBound" 
for making the check box selected  through our code.


  <asp:GridView ID="grdLevels" runat="server" AutoGenerateColumns="False" 
                                        onrowdatabound="grdLevels_RowDataBound" >
                                        <HeaderStyle BackColor="AliceBlue" ></HeaderStyle>
                                        <RowStyle BackColor="AntiqueWhite" ></RowStyle>
                                        <AlternatingRowStyle BackColor="Azure"></AlternatingRowStyle>
                                        <Columns>
                                         <asp:TemplateField>
                                                <ItemTemplate>
                                                 <asp:CheckBox ID="chksel" runat="server"  />
                                                 </ItemTemplate>
                                        </asp:TemplateField>
                                            <asp:BoundField ReadOnly="True" HeaderText="ID" InsertVisible="False"                                                                  DataField="ID">
                                                </asp:BoundField>
                                            <asp:BoundField ReadOnly="True" HeaderText="Name" InsertVisible="False"                                                            DataField="Name">
                                             </asp:BoundField>
                                             <asp:BoundField ReadOnly="True" HeaderText="Status" InsertVisible="False"                                                    DataField="status">
                                             </asp:BoundField>
                                        </Columns>
                                    </asp:GridView>


Now in code behind on page load call the function to bind the data and and make the check box selected if data is present there as per our condition means status is 2


  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            string str = getConstring();
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT  id,name,status FROM log", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                grdLevels.DataSource = dt;
                grdLevels.DataBind();
             
            }
        }

        public string getConstring()
        {
            string constr = ConfigurationManager.ConnectionStrings["constrng"].ConnectionString;
            return constr;
        }

        protected void grdLevels_RowDataBound(object sender, GridViewRowEventArgs e)
        {

          
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    TableCell cell = e.Row.Cells[1];
                    string userid = cell.Text.ToString();

                    DataTable dtsearch = new DataTable();
                    if (userid != "")
                    {
                        string str = getConstring();
                        SqlConnection con = new SqlConnection(str);
                        con.Open();
                        SqlCommand cmd = new SqlCommand("SELECT  id,name,status FROM log where status=2 and                           id=" + userid, con);
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            CheckBox chk = (CheckBox)e.Row.FindControl("chksel");
                            chk.Checked = true;

                        }

                    }

                }
            }


Declare your connection string in web.config file Refer here

0 comments: