Copy and Paste this code to .aspx Page :
<asp:GridView ID="GridView1" runat="server" Width="100%" BackColor="White" BorderColor="White"
BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None"
AutoGenerateColumns="False" Style="font-size: 10pt">
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<PagerStyle BackColor="#669900" ForeColor="White" Font-Bold="true" />
<HeaderStyle BackColor="#669900" Font-Bold="True" ForeColor="#E7E7FF" />
<Columns>
<asp:TemplateField HeaderText="Course" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="id" runat="server" Text='<%#Eval("Row_Id")%>' Visible="false"></asp:Label>
<asp:Label ID="lblgrade" runat="server" Text='<%#Eval("Quali1")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="35px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Grade" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblsubgrade" runat="server" Text='<%#Eval("Quali2")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="45px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Roll No" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblroll" runat="server" Text='<%#Eval("RollNo")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="180px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Father's Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblfname" runat="server" Text='<%#Eval("FName")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="180px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lbldate" runat="server" Text=""></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="70px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbxap" runat="server" AutoPostBack="true" OnCheckedChanged="cbxap_CheckedChanged" />
</ItemTemplate>
<HeaderTemplate>
A/P<asp:CheckBox ID="cbxapall" runat="server" AutoPostBack="true" OnCheckedChanged="cbxapall_CheckedChanged" />
</HeaderTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
and .cs Page Code is given:
Bind GridView:
private void bindgridview()
{
try
{
DataTable dt = app.GetDataTable("select * from tb_Regstd_mst where Center='" + ddlcenter.SelectedItem.Text + "' and Quali1='" + ddlgrade.SelectedItem.Text + "' and Quali2='" + ddlsubgrade.SelectedItem.Text + "' order by RollNo Asc");
GridView1.DataSource = dt;
GridView1.DataBind();
foreach (GridViewRow gv in GridView1.Rows)
{
Label date = (Label)gv.FindControl("lbldate");
date.Text = DateTime.Now.ToShortDateString();
}
if (GridView1.Rows.Count <= 0)
{
btnattd.Visible = false;
lblmsg.Text = "No Student Records Found in this Category.";
}
else
{
lblmsg.Text = "";
btnattd.Visible = true;
}
}
catch (Exception)
{
}
}
On Header Checkbox checkchanged Event :
protected void cbxapall_CheckedChanged(object sender, EventArgs e)
{
try
{
CheckBox chkH = (CheckBox)GridView1.HeaderRow.FindControl("cbxapall");
if (chkH.Checked == true)
{
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox chk = (CheckBox)gv.FindControl("cbxap");
chk.Checked = true;
}
}
else
{
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox chk = (CheckBox)gv.FindControl("cbxap");
chk.Checked = false;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Checkchanged event of each row :
protected void cbxap_CheckedChanged(object sender, EventArgs e)
{
bool isFound = false;
CheckBox cbxall = (CheckBox)GridView1.HeaderRow.FindControl("cbxapall");
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox chkSelect = gvr.FindControl("cbxap") as CheckBox;
if (chkSelect.Checked == false)
{
isFound = true;
break;
}
}
if (isFound)
cbxall.Checked = false;
else
cbxall.Checked = true;
}
<asp:GridView ID="GridView1" runat="server" Width="100%" BackColor="White" BorderColor="White"
BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None"
AutoGenerateColumns="False" Style="font-size: 10pt">
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<PagerStyle BackColor="#669900" ForeColor="White" Font-Bold="true" />
<HeaderStyle BackColor="#669900" Font-Bold="True" ForeColor="#E7E7FF" />
<Columns>
<asp:TemplateField HeaderText="Course" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="id" runat="server" Text='<%#Eval("Row_Id")%>' Visible="false"></asp:Label>
<asp:Label ID="lblgrade" runat="server" Text='<%#Eval("Quali1")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="35px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Grade" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblsubgrade" runat="server" Text='<%#Eval("Quali2")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="45px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Roll No" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblroll" runat="server" Text='<%#Eval("RollNo")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="180px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Father's Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblfname" runat="server" Text='<%#Eval("FName")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="180px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lbldate" runat="server" Text=""></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="70px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbxap" runat="server" AutoPostBack="true" OnCheckedChanged="cbxap_CheckedChanged" />
</ItemTemplate>
<HeaderTemplate>
A/P<asp:CheckBox ID="cbxapall" runat="server" AutoPostBack="true" OnCheckedChanged="cbxapall_CheckedChanged" />
</HeaderTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
and .cs Page Code is given:
Bind GridView:
private void bindgridview()
{
try
{
DataTable dt = app.GetDataTable("select * from tb_Regstd_mst where Center='" + ddlcenter.SelectedItem.Text + "' and Quali1='" + ddlgrade.SelectedItem.Text + "' and Quali2='" + ddlsubgrade.SelectedItem.Text + "' order by RollNo Asc");
GridView1.DataSource = dt;
GridView1.DataBind();
foreach (GridViewRow gv in GridView1.Rows)
{
Label date = (Label)gv.FindControl("lbldate");
date.Text = DateTime.Now.ToShortDateString();
}
if (GridView1.Rows.Count <= 0)
{
btnattd.Visible = false;
lblmsg.Text = "No Student Records Found in this Category.";
}
else
{
lblmsg.Text = "";
btnattd.Visible = true;
}
}
catch (Exception)
{
}
}
On Header Checkbox checkchanged Event :
protected void cbxapall_CheckedChanged(object sender, EventArgs e)
{
try
{
CheckBox chkH = (CheckBox)GridView1.HeaderRow.FindControl("cbxapall");
if (chkH.Checked == true)
{
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox chk = (CheckBox)gv.FindControl("cbxap");
chk.Checked = true;
}
}
else
{
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox chk = (CheckBox)gv.FindControl("cbxap");
chk.Checked = false;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Checkchanged event of each row :
protected void cbxap_CheckedChanged(object sender, EventArgs e)
{
bool isFound = false;
CheckBox cbxall = (CheckBox)GridView1.HeaderRow.FindControl("cbxapall");
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox chkSelect = gvr.FindControl("cbxap") as CheckBox;
if (chkSelect.Checked == false)
{
isFound = true;
break;
}
}
if (isFound)
cbxall.Checked = false;
else
cbxall.Checked = true;
}
No comments:
Post a Comment