Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want that check box in header row automatically checked when all data row of gridview are checked.

What I have tried:

ASP.NET
<asp:GridView ID="gv" Width="90%" runat="server" Visible="false" AutoGenerateColumns="false"
    PageSize="51" OnRowDataBound="gv_RowDataBound" OnPageIndexChanging="gv_PageIndexChanging" >
    <Columns>
        <asp:TemplateField HeaderText="S.no" ItemStyle-Width="5%">
            <ItemTemplate>
                <%#Container.DataItemIndex+1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="FPS Code" ItemStyle-Width="20%">
            <ItemTemplate>
                <asp:Literal ID="lblCode" runat="server" Text='<%#Eval("Code")%>'></asp:Literal>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Details" ItemStyle-Width="20%">
            <ItemTemplate>
                <asp:Label ID="lblDetails" runat="server" Text='<%#Eval("Details")%>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="DBT Option" ItemStyle-Width="20%">
            <HeaderTemplate>
                <asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkboxSelectAll_CheckedChanged" />
            </HeaderTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemTemplate>
                <asp:CheckBox ID="chkFPS" runat="server" AutoPostBack="true" OnCheckedChanged="chkFPS_CheckedChanged" />                                                                                         </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

C#
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
         if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataTable dt = ViewState["dt"] as DataTable;
                
                string lblCode = ((Literal)e.Row.FindControl("lblCode")).Text;
                CheckBox ChkBoxRows = (CheckBox)e.Row.FindControl("chkFPS");
                DataRow[] row = dt.Select("FPS_Code='" + lblCode + "'", "FPSDBT");
                if (row[0]["FPSDBT"].ToString() == "Yes")
                    ChkBoxRows.Checked = true;
                else
                    ChkBoxRows.Checked = false;
                
            }            
            else if (e.Row.RowType == DataControlRowType.Header)
            {
                bool flage = true;
                //this table shows all  record from data base and column "FPSDBT" shows that recored should be checked based on "FPSDBT"
                DataTable dt = ViewState["dt"] as DataTable; 
                CheckBox chkboxSelectAll= (CheckBox)e.Row.FindControl("chkboxSelectAll");
                DataRow[] row = dt.Select("1=1", "FPSDBT");                
                for (int j = 0; j < row.Length; j++)
                {
                    if (row[j]["FPSDBT"].ToString() == "No")
                    {
                        flage = false;
                        break;
                    }
                }
                if (flage)                
                    chkboxSelectAll.Checked = true;
                   
            }

    }
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            
            BindGridView_gv();
            grdFPSDBT.PageIndex = e.NewPageIndex;
            grdFPSDBT.DataBind();

        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Some internal error occurred. Please try after some time. !!! ');</script>", false);
        }
    }


It is running fine.
But when record number more than 51 is "No"
i.e. row[52]["FPSDBT"] is "No". Means 52th record is not checked.
then I want check box("chkboxSelectAll") in header row in page number 1 (51 records) should be checked
and check box("chkboxSelectAll") in page number 2 should not be checked.
Posted
Updated 17-Jul-16 21:30pm
v4
Comments
ZurdoDev 15-Jul-16 7:55am    
I don't understand your question.
Member 7909353 15-Jul-16 8:08am    
Column "FPSDBT" show datarow record is checked or not.
If "FPSDBT" is "Yes" then checked other wise not checked.
If I have 100 records and page size is 51.
suppose "FPSDBT" is "No" of 52th record then I want check box in header row should be checked in page number 1 and not checked in page number 2
ZurdoDev 15-Jul-16 8:33am    
So, implement the paging event and clear the checkbox.
saeed rajabi 15-Jul-16 9:49am    
take a look http://stackoverflow.com/questions/15293641/asp-net-gridview-checkall-and-uncheckall-in-a-single-header-checkbox

try this
C#
int count = 51;
count = row.Length <= 51 ? row.Length : count;
for (int j = 0; j < count; j++)
{
  .
  .

your gridview doesnt have pagination event, so it should work fine... if you are handling pagination event then you will have rewrite the code to find the checked state on that particular page using PageIndex Property [^] and PageCount Property [^]
 
Share this answer
 
Comments
Member 7909353 15-Jul-16 8:23am    
not working
Karthik_Mahalingam 15-Jul-16 8:25am    
so now you need to handle the pagination?
i will come up with a solution.
Member 7909353 18-Jul-16 3:31am    
Thanks it is done by pPageIndex and PageCount property
Karthik_Mahalingam 18-Jul-16 3:39am    
good. :)
C#
else if (e.Row.RowType == DataControlRowType.Header)
            {
int count=0;
                bool flage = true;
                
                DataTable dt = ViewState["dt"] as DataTable; 
                CheckBox chkboxSelectAll= (CheckBox)e.Row.FindControl("chkboxSelectAll");
                DataRow[] row = dt.Select("1=1", "FPSDBT");   


int pageindex = grdFPSDBT.PageIndex;
                count = grdFPSDBT.PageSize * (++pageindex);
                count = count > row.Length ? row.Length : count; 
             
                 for (int j = grdFPSDBT.PageIndex * grdFPSDBT.PageSize; j < count; j++)
                {
                    if (row[j]["FPSDBT"].ToString() == "No")
                    {
                        flage = false;
                        break;
                    }
                }
                if (flage)                
                    chkboxSelectAll.Checked = true;
                   
            }
 
Share this answer
 

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