Hi kankon,
Check this example. Now please take its reference and correct your code.
HTML
<asp:CheckBox ID="jop" Text="الوظيفة" runat="server" OnCheckedChanged="OnCheckedChanged"
AutoPostBack="true" Checked="true" CssClass="auto-style26" />
<asp:CheckBox ID="fileid" Text="رقم الملف" runat="server" OnCheckedChanged="OnCheckedChanged1"
AutoPostBack="true" Checked="true" CssClass="auto-style26" />
<asp:CheckBox ID="markzel3mel" Text="القسم" runat="server" OnCheckedChanged="OnCheckedChanged2"
AutoPostBack="true" Checked="true" CssClass="auto-style26" />
<asp:CheckBox ID="eldarajah" Text="الدرجة" runat="server" OnCheckedChanged="OnCheckedChanged3"
AutoPostBack="true" Checked="true" CssClass="auto-style26" />
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-BackColor="white" AutoGenerateColumns="False"
CssClass="Grid" Font-Names="Arial" Font-Size="11pt" ShowHeaderWhenEmpty="True" Width="100%" Style="font-size: medium">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" Checked="True" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="True" onclick="Check_Click(this)" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="5px" />
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="م">
<ItemStyle HorizontalAlign="Center" Width="10px" Font-Bold="True" Font-Size="Large" />
</asp:BoundField>
<asp:TemplateField HeaderText="الاسم">
<ItemTemplate>
<asp:Label Text='<%# Eval("name") %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="200px" HorizontalAlign="Right"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="الرقم المدني">
<ItemTemplate>
<asp:Label Text='<%# Eval("civilid") %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="100px" HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="الوظيفة">
<ItemTemplate>
<asp:Label Text='<%# Eval("jop") %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="150px" HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="رقم الملف">
<ItemTemplate>
<asp:Label Text='<%# Eval("fileid") %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="70px" HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="القسم">
<ItemTemplate>
<asp:Label Text='<%# Eval("markazel3mal") %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="150px" HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="الدرجة">
<ItemTemplate>
<asp:Label Text='<%# Eval("eldarajah") %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="150px" HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label Text='<%# Eval("blockemergency") %>' runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="1px" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<div align="center">
No records found.No records found.
</div>
</EmptyDataTemplate>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("id"),
new DataColumn("name"),
new DataColumn("civilid"),
new DataColumn("jop"),
new DataColumn("fileid"),
new DataColumn("markazel3mal"),
new DataColumn("eldarajah"),
new DataColumn("blockemergency") });
dt.Rows.Add(1, "John Hammond", "1", "jop 1", "file 1", "markazel3mal 1", " eldarajah", "blockemergency 1");
dt.Rows.Add(2, "Mudassar Khan", "2", "jop 2", "file 2", "markazel3mal 2", " eldarajah", "blockemergency 2");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnCheckedChanged(object sender, EventArgs e)
{
GridView1.Columns[4].Visible = (sender as CheckBox).Checked;
}
protected void OnCheckedChanged1(object sender, EventArgs e)
{
GridView1.Columns[5].Visible = (sender as CheckBox).Checked;
}
protected void OnCheckedChanged2(object sender, EventArgs e)
{
GridView1.Columns[6].Visible = (sender as CheckBox).Checked;
}
protected void OnCheckedChanged3(object sender, EventArgs e)
{
GridView1.Columns[7].Visible = (sender as CheckBox).Checked;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("id"),
New DataColumn("name"),
New DataColumn("civilid"),
New DataColumn("jop"),
New DataColumn("fileid"),
New DataColumn("markazel3mal"),
New DataColumn("eldarajah"),
New DataColumn("blockemergency")})
dt.Rows.Add(1, "John Hammond", "1", "jop 1", "file 1", "markazel3mal 1", " eldarajah", "blockemergency 1")
dt.Rows.Add(2, "Mudassar Khan", "2", "jop 2", "file 2", "markazel3mal 2", " eldarajah", "blockemergency 2")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
GridView1.Columns(4).Visible = (TryCast(sender, CheckBox)).Checked
End Sub
Protected Sub OnCheckedChanged1(ByVal sender As Object, ByVal e As EventArgs)
GridView1.Columns(5).Visible = (TryCast(sender, CheckBox)).Checked
End Sub
Protected Sub OnCheckedChanged2(ByVal sender As Object, ByVal e As EventArgs)
GridView1.Columns(6).Visible = (TryCast(sender, CheckBox)).Checked
End Sub
Protected Sub OnCheckedChanged3(ByVal sender As Object, ByVal e As EventArgs)
GridView1.Columns(7).Visible = (TryCast(sender, CheckBox)).Checked
End Sub