This article will show you how you hide or show gridview row
by column name at runtime in asp.net using c#.net. In this we will display to
bind value total at run time in asp.net using c#.net.
Some of my previous articles are as follows : Display
Grand Total In Gridview Footer On RowDataBound In Asp.Net Using C#.Net, Detect
Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using
C#.Net, Merge
Header or Apply ColumnSpan to Header of GridView In Asp.Net Using C#.Net, Merge
Footer or Apply ColumnSpan to Footer of GridView In Asp.Net Using C#.Net, Display
Selected Row Value Of GridView In JavaScript Alert Message Asp.Net Using C#.Net,
Find
Selected Row Value Of GridView on Button Click In Asp.Net Using C#.Net, Detect
Checked CheckBox Indise GridView on Button Click In Asp.Net Using C#.Net, Auto
Generated Row No GridView In Asp.Net Using C#.Net.
So for this article first we will create a new asp.net
application and add the below code in the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Hide Or
Show Gridview Column By Column Name at RunTime In Asp.Net Using C#.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False"
ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName"
runat="server"
Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<FooterStyle Font-Bold="True"
/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblNamAddress"
runat="server"
Text='<%# Eval("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label ID="lblUserType"
runat="server"
Text='<%# Eval("UserType") %>'></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right"
/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Earning">
<ItemTemplate>
<asp:Label ID="lbltotal"
runat="server"
Text='<%# Eval("Earning") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
In above code I have use itemtemplate to bind the gridview
fields and used label control to display the value using Eval. In this i have
provided value to each and every header. I will detect the header and make it
hidden at run time.
Now check the code to bind and display no of pages.
using System;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using System.Collections;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
DataTable
objdt = new DataTable();
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GetDistinctValue();
}
}
private
void GetDistinctValue()
{
SqlConnection
con = new SqlConnection(System.Configuration.
ConfigurationManager.ConnectionStrings["con"].ToString());
try
{
string
query = "select * from UserDetail;";
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
GridView1.DataSource =
objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
protected
void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
string
columnName = "Earning";
var
columnToHide = GridView1.Columns.Cast<DataControlField>().FirstOrDefault(m
=> m.HeaderText == columnName);
if
(columnToHide != null)
{
//True
for Show and False for Hide
columnToHide.Visible = false;
}
}
}
}
|
On above code I have used a function to bind the data. Now I
have I have added code to hide column by using . Now check the below code to hide
the column at run time.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
string
columnName = "Earning";
var
columnToHide = GridView1.Columns.Cast<DataControlField>().FirstOrDefault(m
=> m.HeaderText == columnName);
if
(columnToHide != null)
{
//True
for Show and False for Hide
columnToHide.Visible = false;
}
}
|
In above code I have hide the linq query to hide the column
by name. Now we have done run the application to check the output.
Here is the output without displaying hiding the column.
Here is the output with hidden column.
DOWNLOAD
0 comments:
Please let me know your view