I work on sp.net web forms . I face issue I can’t add column checkbox to grid view as Header column .
so when header column checked then all rows checked on grid view
and when header column not checked then all rows not checked on grid view .
meaning all rows grid view will depend on column header checked or not .
so How to do that using jQuery or java script or CSS .
<div class="row">
<div class="table-responsive">
<asp:GridView ID="GridViewSearchData" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table" HeaderStyle-BackColor="#172b4d" ForeColor="White" OnDataBound="GridViewSearchData_OnDataBound" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" PageSize="1">
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkSel" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="BranchCode" HeaderText="BranchCode" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="OrderNo" HeaderText="OrderNo" ItemStyle-Width="120px" />
<asp:BoundField DataField="OrderType" HeaderText="OrderType" />
<asp:BoundField DataField="Printer_name" HeaderText="Printer_name" />
<asp:BoundField DataField="EntredDatetime" HeaderText="EntredDatetime" />
<asp:BoundField DataField="Id" HeaderText="Id" />
</Columns>
</asp:GridView>
</div>
</div>
for grid view data source
protected void Search_Click(object sender, EventArgs e)
{
DataTable dt;
dt = busiObj.DisplayAllSearchData();
if (dt != null && dt.Rows.Count > 0)
{
GridViewSearchData.DataSource = dt;
GridViewSearchData.DataBind();
}
}
image for expected result as red color first column
2
Answers
example using jquery:
JavaScript:
First up, how to add "any" control to the header.
It works very much like say any item template.
Start a "item templte", and INSIDE that, drop in a header template.
So, we have this now:
And we wired up a click event for the check box. So, the code is now this:
And now we get/see this:
So, above is real clean, VERY easy.
of course, we could do this code 100% client side with JavaScript.
So, now out button (header) has no server side click, nor a post-back.
So, this:
And the js code is this: (jQuery)
And now the result is really the same, but no server side post-back.
However, either way? Note the use of the Header template –
And last but not least?
Might as well include the code to load the gv.