This example shows how to Hide GridView Columns in normal mode in asp.net and set them to visible when gridview is in edit mode. I am hiding the ID column when gridview loads and setting this column to visible when user clicks on the edit link button.
For this i am using ObjectDataSource to populate the grid and hiding columns in RowDataBound Event.
<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")%>'/>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtID" Visible="true" runat="server"
Text='<%#Eval("ID")%>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'/>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtName" Visible="true" runat="server"
Text='<%#Eval("Name")%>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server"
Text='<%#Eval("Location")%>'/>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtLocation" Visible="true" runat="server"
Text='<%#Eval("Location")%>'/>
</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>
We need to write following code to hide columns in RowDataBound event
C# CODE
01
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
02
{
03
//Check whether gridview is in edit mode or nor
04
if
(GridView1.EditIndex >= 0)
05
{
return
; }
06
//Check row state of gridview whether it is data row or not
07
if
((e.Row.RowState == DataControlRowState.Normal
08
|| e.Row.RowState == DataControlRowState.Alternate)
09
&& (e.Row.RowType == DataControlRowType.DataRow
10
|| e.Row.RowType == DataControlRowType.Header))
11
{
12
//Now set the visibility of cell we want to hide to false
13
e.Row.Cells[1].Visible =
false
;
14
}
15
}
16
17
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
18
{
19
GridView1.EditIndex = e.NewEditIndex;
20
GridView1.DataBind();
21
}
VB.NET
01
Protected
Sub
GridView1_RowDataBound(sender
As
Object
, e
As
GridViewRowEventArgs)
02
'Check whether gridview is in edit mode or nor
03
If
GridView1.EditIndex >= 0
Then
04
Return
05
End
If
06
'Check row state of gridview whether it is data row or not
07
If
(e.Row.RowState = DataControlRowState.Normal
OrElse
e.Row.RowState = DataControlRowState.Alternate)
AndAlso
(e.Row.RowType = DataControlRowType.DataRow
OrElse
e.Row.RowType = DataControlRowType.Header)
Then
08
'Now set the visibility of cell we want to hide to false
09
e.Row.Cells(1).Visible =
False
10
End
If
11
End
Sub
12
13
Protected
Sub
GridView1_RowEditing(sender
As
Object
, e
As
GridViewEditEventArgs)
14
GridView1.EditIndex = e.NewEditIndex
15
GridView1.DataBind()
16
End
Sub
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