This example shows how to hide columns of gridview in normal display mode and set them to visible when gridview is in edit mode
here i am hiding the ID column when gridview loads in normal mode and setting this column to visible when user clicks on the edit link button of gridview
For this i am using ObjectDataSource to populate the grid and hiding the columns in RowDataBound Event of gridview
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1"
OnRowDataBound="GridView1_RowDataBound"
OnRowEditing="GridView1_RowEditing" >
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID"
runat="server"
Text='<%#Eval("ID")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtID" Visible="true"
runat="server"
Text='<%#Eval("ID")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName"
runat="server"
Text='<%#Eval("Name")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtName" Visible="true"
runat="server"
Text='<%#Eval("Name")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server"
Text='<%#Eval("Location")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtLocation"
Visible="true"
runat="server"
Text='<%#Eval("Location")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1"
runat="server"
InsertMethod="Insert"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetData"
TypeName="DataSet1TableAdapters.TestTableAdapter"
UpdateMethod="UpdateQuery">
<InsertParameters>
<asp:Parameter Name="ID" Type="Decimal" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Location" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Location" Type="String" />
<asp:Parameter Name="ID" Type="Decimal" />
</UpdateParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
And the code behind for this goes like this
We need to write code to hide columns in RowDataBound event
public protected void GridView1_RowDataBound
(object sender, GridViewRowEventArgs e)
{
//Check whether gridview is in edit mode or nor
if (GridView1.EditIndex >= 0)
{ return; }
//Check row state of gridview whether it is data row or not
if ((e.Row.RowState == DataControlRowState.Normal
|| e.Row.RowState == DataControlRowState.Alternate)
&& (e.Row.RowType == DataControlRowType.DataRow
|| e.Row.RowType == DataControlRowType.Header))
{
//Now set the visibility of cell we want to hide to false
e.Row.Cells[1].Visible = false;
}
}
protected void GridView1_RowEditing
(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
}
Download the sample code attached

Hide GridView Columns In ASP.NET
Posted by
amiT jaiN
If you like this post than join us or share
Labels: C#, GridView, ObjectDataSource
Subscribe to:
Post Comments (Atom)
Tags
ASP.NET
C#
GridView
VB.NET
AJAX
Sql Server
JavaScript
FindControl
EditItemTemplate
WinForms
Windows Froms
Session
Visual studio
Web.config
Authentication
ItemTemplate
DropDownList
Forms Authentication
Performance Optimization
Blogger Tricks
Crystal Reports
QueryString
SqlCommand
XML
AutoComplete Extender
BloggerTips
DetailsView
Excel
FileUpload
MembershipProvider
ObjectDataSource
DataList
IIS
LoginControl
ModalPopUpExtender
TraceMobileNumber
Update Panel
Web Service
iTextSharp
AppFabric
Cross Page Posting
DataKeyNames
jQuery
Cookies
FooterTemplate
MasterPage
Progress Template
Server.Transfer
Submit Form
Blog Archive
-
►
2012
(28)
-
►
February
(14)
- ToolkitScriptManager On MasterPage To Use AjaxCont...
- PrintPreviewDialog In Windows Forms DataGridView C...
- Print Printing DataGridView In WinForms C# VB.NET
- Hide Disable Commandfield Or ButtonField In GridVi...
- ExecuteNonQuery ExecuteScalar ExecuteReader Exampl...
- Read Write XML File Using C# VB.NET In Asp.Net
- Check UserName Email Availability In Asp.Net Using...
- SqlCommand ExecuteXmlReader C# VB.NET Asp.Net
- ExecuteScalar Example In Asp.Net C# VB.NET
- ExecuteReader Example Asp.Net C# VB.NET
- SqlCommand ExecuteNonQuery Example C# VB.NET Asp.N...
- Visual Studio Keyboard Shortcuts
- Asp.Net Ajax FilteredTextBox Extender Example
- Asp.Net Maintain ScrollPosition OnPostBack In Chro...
-
►
January
(14)
- Browser Detection In Asp.Net
- Export Crystal Reports To PDF Word Excel In Asp.Ne...
- Asp.Net AdRotator With Timer Example To Rotate Ads...
- Download File From Server In Asp.Net
- AspNet CompareValidator With Date Example
- Create User Registration Form In AspNet
- Visual Studio Remove Unused References With Organi...
- Get RowIndex In GridView RowCommand Event Using Da...
- Save Store Files In SqlServer Database AspNet
- Nested GridView In Asp.Net GridView Inside GridVie...
- Add License Agreement In Visual Studio Setup Proje...
- Ajax Asp.Net PasswordStrength Example
- Invalid Formatetc Structure Ajax Asp.Net Error
- Create User Programmatically Using Membership In A...
-
►
February
(14)
-
►
2011
(46)
-
►
December
(13)
- CreateUserWizard Email Verification Or Confirmatio...
- Login Page Example Using Login Control In Asp.Net
- Set Session TimeOut In Asp.Net Using WebConfig IIS...
- Remove Delete Duplicate Rows/Records From DataTabl...
- DropDownList In DetailsView EditItemTemplate
- A Potentially Dangerous Request.Form Value Was Det...
- Write Modify Web.Config Programmatically At Run Ti...
- GridView XML Edit Delete Insert Update
- Upload And Read Excel File In Asp.Net
- VisualStudio SetupProject Updates Version Already ...
- Read CSV File And Save To SQL Server In ASP.NET C#...
- GridView XMLDataSource Example
- Add Controls Dynamically WinForms WindowsFroms C# ...
-
►
December
(13)
-
▼
2008
(20)
-
▼
December
(11)
- Change Mode Of DetailsView FormView From Default M...
- Hide GridView Columns In ASP.NET
- Export GridView to Pdf-ASP.NET
- Find IP Address in ASP.NET Behind Proxy
- OpenFileDialog in winforms windows forms applicati...
- User validation authentication using session in AS...
- ASP.NET Search Records In GridView Footer And High...
- Register Dlls Assembly Custom User Controls Ascx I...
- ASP.NET TextBox Submit Form On Enter Key DefaultBu...
- Search Records In GridView And Highlight Results A...
- Detect Browser Refresh Avoid events fire in ASP.NE...
-
▼
December
(11)
Topics
- AJAX
- AppFabric
- ASP.NET
- Authentication
- AutoComplete Extender
- Blogger Tricks
- BloggerTips
- C#
- Cookies
- Cross Page Posting
- Crystal Reports
- DataKeyNames
- DataList
- DetailsView
- DropDownList
- EditItemTemplate
- Excel
- FileUpload
- FindControl
- FooterTemplate
- Forms Authentication
- GridView
- IIS
- ItemTemplate
- iTextSharp
- JavaScript
- jQuery
- LoginControl
- MasterPage
- MembershipProvider
- ModalPopUpExtender
- ObjectDataSource
- Performance Optimization
- Progress Template
- QueryString
- Server.Transfer
- Session
- Sql Server
- SqlCommand
- Submit Form
- TraceMobileNumber
- Update Panel
- VB.NET
- Visual studio
- Web Service
- Web.config
- Windows Froms
- WinForms
- XML
Popular Posts
- Trace Mobile Number Location Operator in India
- Trace Mobile Number In India
- Ajax ModalPopUpExtender Example
- Crystal reports in ASP.NET
- FileUpload Save Images In SqlServer Database in ASP.NET
- Display Images In GridView From DataBase Asp.Net
- Shopping Cart Example Code In ASP.NET GridView DataList
- GridView Examples In ASP.NET 2.0 3.5 4.0
- Insert Update Edit Delete record in GridView
- Disable Browser Back Button Using Javascript ASP.NET

16 comments:
Thanx i was looking for this , Google brought me here
i want to download source code of this artical
@himanshu :
You can download the code now, i've fixed the download link
your posts are nice.
how to set readonly to id field in gridview when editing
студент видео онлайн http://free-3x.com/ малолетние порно фотомодели free-3x.com/ смотреть порно школа [url=http://free-3x.com/]free-3x.com[/url]
порно школьниц смотреть бесплатно видео
порево порно
видеоролики эротика просмотр
видео занятие сексом
порно мувики бесплатно
скачать порнофильм taboo бесплатно
порно жёсткое взрослые
эротическое видео ролики скачать
московский секс
пизда рвётся фото
hi
here u have check for Edit mode ,Y?
Please let me know
and i have another question for sqlserver
i have table ABC
in that
Vehiclename driver
=====================
Car 1,2
Car 1,3,5
Car 1,2,3,4
Scooter 2,4,6
Scooter 2
now i want result like this
Vehiclename Driver
=======================
Car 1,2,3,4,5
Scooter 2,4,6
Please give query for this
Hello. And Bye.
Да уж… Жизнь – как вождение велосипеда. Чтобы сохранить равновесие ты должен двигаться.
how to display image in gridview after retrieving binary data of an image file which is stored in the database
Thank your for your solution amit !!
And it will also hide the column header?
@Above : Yes, it Will
please tell me does windows xp sp2 support asp.net 4.0 ? does asp.net 4.0 work successfully on windows xp sp2 ? My email id is : hiralvyas1986@yahoo.in
hello i want to know that how to bind controls to the gridview
@Above: Please refer GridView Examples in ASP.NET 2.0 3.5 to know how to bind controls in GridView
Post a Comment