Tuesday, 3 May 2016

Implement check all checkbox functionality in ASP.Net GridView control using JavaScript

Implement check all checkbox functionality in ASP.Net GridView control using JavaScript

database Structure of tbl_login 





















First add below script between <head> tag

<script type="text/javascript" language="javascript">
        function CheckAllEmp(Checkbox) {
            var GridVwHeaderChckbox = document.getElementById("<%=grdata.ClientID %>");
            for (i = 1; i < GridVwHeaderChckbox.rows.length; i++) {
                GridVwHeaderChckbox.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = Checkbox.checked;
            }
        }
    </script> 

Note: where grdata is gridview id.

Now add gridview in your aspx page

<asp:GridView runat="server" ID="grdata" AutoGenerateColumns="false" DataKeyNames="UserName" class="table table-striped table-bordered table-hover">
 <Columns>
    <asp:TemplateField>
        <HeaderTemplate>
              <asp:CheckBox ID="chkHeader" runat="server" onclick="CheckAllEmp(this);" />
        </HeaderTemplate>
     <ItemTemplate>
        <asp:CheckBox runat="server" ID="chck" />
      </ItemTemplate>
  </asp:TemplateField>
   <asp:TemplateField HeaderText="Name">
        <ItemTemplate>
           <asp:Label runat="server" ID="lblName" Text='<%#Eval("Name") %>'></asp:Label>
         </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Mobile">
     <ItemTemplate>
       <asp:Label runat="server" ID="lblMobile" Text='<%#Eval("Mobile") %>'></asp:Label>
       </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField HeaderText="ID">
       <ItemTemplate>
         <asp:Label runat="server" ID="lblid" Text='<%#Eval("UserName") %>'></asp:Label>
       </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField HeaderText="Address">
       <ItemTemplate>
      <asp:Label runat="server" ID="lblAddress" Text='<%#Eval("Address") %>'></asp:Label>
    </ItemTemplate>
   </asp:TemplateField>
  <asp:TemplateField HeaderText="Email">
    <ItemTemplate>
       <asp:Label runat="server" ID="lblEmail" Text='<%#Eval("emailid") %>'></asp:Label>
     </ItemTemplate>
 </asp:TemplateField>
</Columns>

</asp:GridView>

In .CS FILE add this code for bind gridview 


void bind()
    {
        try
        {
            Connection.con.Open();
            SqlCommand cmd = new SqlCommand("select * from tbl_Login where Role ='Sr Coordinator' order by id desc", Connection.con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                grdata.DataSource = dt;
                grdata.DataBind();
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                grdata.DataSource = dt;
                grdata.DataBind();
                int count = grdata.Rows[0].Cells.Count;
                grdata.Rows[0].Cells.Clear();
                grdata.Rows[0].Cells.Add(new TableCell());
                grdata.Rows[0].Cells[0].ColumnSpan = count;
                grdata.Rows[0].Cells[0].Text = "There is no record in your DataBase..";
            }
        }
        catch (Exception ex)
        {
           
        }
        finally
        {
            Connection.con.Close();
        }
    }

Now i told you how to hold  value in datatable of selected column of gridview


Use the following method code of hold data in datatable which are use easily
  

  public void getSelectRows()
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[5] { new DataColumn("EmployeeID"), new DataColumn("Name"), new DataColumn("Mobile"), new DataColumn("Email"), new DataColumn("Address")});
        foreach (GridViewRow row in grdata.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chck") as CheckBox);
                Label lblName1 = (row.Cells[0].FindControl("lblName") as Label);
                Label lblId = (row.Cells[0].FindControl("lblid") as Label);
                Label lblmobile = (row.Cells[0].FindControl("lblMobile") as Label);

                Label lblAdd = (row.Cells[0].FindControl("lblAddress") as Label);
                Label lblEmail = (row.Cells[0].FindControl("lblEmail") as Label);
             

                if (chkRow.Checked)
                {
                  
                    string EmployeeID = lblId.Text;
                    string Name = lblName1.Text;
                    string Mobile = lblmobile.Text;
                    string Address = lblAdd.Text;
                    string email = lblEmail.Text;
                
                    //string Date = DateTime.Now.ToString("dd/MMM/yyyy");

                    dt.Rows.Add(EmployeeID, Name, Mobile, email, Address);
                }
            }
        }
    }

Above code use can use as select all checkbox in gridview in asp.net.

No comments:

Post a Comment