Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox>
<br />
<asp:GridView ID="GVBigbaprdwiserpt" runat="server" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"
GridLines="Horizontal" AutoGenerateColumns="false" Font-Names="Arial" CssClass="HeaderFreez" ShowHeader="true" Font-Size="11pt"
AlternatingRowStyle-BackColor="#C2D69B" class="grid_scroll" PageSize="5" ShowHeaderWhenEmpty="True" ShowFooter="True" Width="600px">
<AlternatingRowStyle BackColor="#F7F7F7" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
<Columns>
<asp:TemplateField HeaderText="Artical">
<ItemTemplate>
<asp:Label ID="Artical" runat="server" Text='<%#Bind("Artical")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Section">
<ItemTemplate>
<asp:Label ID="Section" runat="server" Text='<%#Bind("Section")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Catagory">
<ItemTemplate>
<asp:Label ID="catagory" runat="server" Text='<%#Bind("catagory")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalText" Text="Total" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:Label ID="QTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemTemplate>
<asp:Label ID="Weight" runat="server" Text='<%#Bind("Weight")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal1" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Floor">
<ItemTemplate>
<asp:Label ID="FName" runat="server" Text='<%#Bind("FName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Artical"),
new DataColumn("Section"),
new DataColumn("catagory"),
new DataColumn("QTY"),
new DataColumn("Weight"),
new DataColumn("FName")
});
dt.Rows.Add("Artical 1", "Section 1", "catagory 1", 1, 1, "John Hammond");
dt.Rows.Add("Artical 2", "Section 2", "catagory 2", 2, 2, "Mudassar Khan");
dt.Rows.Add("Artical 3", "Section 3", "catagory 3", 3, 3, "Suzanne Mathews");
dt.Rows.Add("Artical 4", "Section 4", "catagory 4", 4, 4, "Robert Schidner");
GVBigbaprdwiserpt.DataSource = dt;
GVBigbaprdwiserpt.DataBind();
}
protected void OnTextChanged(object sender, EventArgs e)
{
this.BindGrid();
// Validate your textbox as per your condition.
if (TextBox1.Text == "1")
{
GVBigbaprdwiserpt.Columns[2].Visible = false;
}
else
{
GVBigbaprdwiserpt.Columns[2].Visible = true;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() { New DataColumn("Artical"), New DataColumn("Section"), New DataColumn("catagory"), New DataColumn("QTY"), New DataColumn("Weight"), New DataColumn("FName")})
dt.Rows.Add("Artical 1", "Section 1", "catagory 1", 1, 1, "John Hammond")
dt.Rows.Add("Artical 2", "Section 2", "catagory 2", 2, 2, "Mudassar Khan")
dt.Rows.Add("Artical 3", "Section 3", "catagory 3", 3, 3, "Suzanne Mathews")
dt.Rows.Add("Artical 4", "Section 4", "catagory 4", 4, 4, "Robert Schidner")
GVBigbaprdwiserpt.DataSource = dt
GVBigbaprdwiserpt.DataBind()
End Sub
Protected Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.BindGrid()
' Validate your textbox as per your condition.
If TextBox1.Text = "1" Then
GVBigbaprdwiserpt.Columns(2).Visible = False
Else
GVBigbaprdwiserpt.Columns(2).Visible = True
End If
End Sub