stretch73
asked on
GridView EditTemplate
I am working with a gridview in which one of the fields in each row is editable. Everything works fine, I'm just wondering if there is a way to hide the field until the user clicks the 'Edit' button. There's never going to be anything in this column and it seems dumb to have it always displaying.
Thanks in advance,
N
Thanks in advance,
N
Convert the field/column to a templated column and then just remove or set the visible property of the ItemTemplate controls that are displayed in read only mode to false.
ASKER
Here's what I have:
<asp:TemplateField ItemStyle-HorizontalAlign= "center" HeaderText="NBID">
<ItemTemplate>
<asp:Label ID="lblStandardID" Text='<%# Eval("Standard_ID") %>' runat="server" Visible="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNBID" MaxLength="7" Width="50px" Text='<%# Bind("Standard_ID") %>' runat="server" /><br />
</EditItemTemplate>
</asp:TemplateField>
but I can still see the column.
<asp:TemplateField ItemStyle-HorizontalAlign=
<ItemTemplate>
<asp:Label ID="lblStandardID" Text='<%# Eval("Standard_ID") %>' runat="server" Visible="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNBID" MaxLength="7" Width="50px" Text='<%# Bind("Standard_ID") %>' runat="server" /><br />
</EditItemTemplate>
</asp:TemplateField>
but I can still see the column.
Ok, I misunderstood the scenario a bit. It looks like you can't do it declaratively, you'll have to do it in the code behind. Try adding the following the code to the GridView's Item RowDataBound event:
If GridView1.EditIndex > -1 Then
GridView1.Columns(0).Visib le = True
Else
GridView1.Columns(0).Visib le = False
End If
Replace "0" with the number of your column starting at 0 and counting left to right (Top to bottom in source view) . Keeping in mind that columns that aren't visible are still counted.
If GridView1.EditIndex > -1 Then
GridView1.Columns(0).Visib
Else
GridView1.Columns(0).Visib
End If
Replace "0" with the number of your column starting at 0 and counting left to right (Top to bottom in source view) . Keeping in mind that columns that aren't visible are still counted.
ASKER
That blew up:
Object reference not set to an instance of an object.
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls. BaseValida tor.OnInit (EventArgs e) +41
System.Web.UI.Control.Init Recursive( Control namingContainer) +459
System.Web.UI.Control.Init Recursive( Control namingContainer) +271
System.Web.UI.Control.Init Recursive( Control namingContainer) +271
System.Web.UI.Control.Adde dControl(C ontrol control, Int32 index) +327
System.Web.UI.ControlColle ction.Add( Control child) +263
System.Web.UI.WebControls. RowControl Collection .Add(Contr ol child) +55
System.Web.UI.ControlColle ction.AddA t(Int32 index, Control child) +44
System.Web.UI.WebControls. RowControl Collection .AddAt(Int 32 index, Control child) +60
System.Web.UI.WebControls. TableRowCo llection.A ddAt(Int32 index, TableRow row) +48
System.Web.UI.WebControls. TableRowCo llection.A dd(TableRo w row) +32
System.Web.UI.WebControls. GridView.C reateRow(I nt32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +179
System.Web.UI.WebControls. GridView.C reateChild Controls(I Enumerable dataSource, Boolean dataBinding) +4305
System.Web.UI.WebControls. CompositeD ataBoundCo ntrol.Perf ormDataBin ding(IEnum erable data) +88
System.Web.UI.WebControls. GridView.P erformData Binding(IE numerable data) +38
System.Web.UI.WebControls. DataBoundC ontrol.OnD ataSourceV iewSelectC allback(IE numerable data) +126
System.Web.UI.DataSourceVi ew.Select( DataSource SelectArgu ments arguments, DataSourceViewSelectCallba ck callback) +98
System.Web.UI.WebControls. DataBoundC ontrol.Per formSelect () +153
System.Web.UI.WebControls. BaseDataBo undControl .DataBind( ) +99
System.Web.UI.WebControls. GridView.D ataBind() +23
System.Web.UI.WebControls. BaseDataBo undControl .EnsureDat aBound() +92
System.Web.UI.WebControls. BaseDataBo undControl .OnPreRend er(EventAr gs e) +33
System.Web.UI.WebControls. GridView.O nPreRender (EventArgs e) +74
System.Web.UI.Control.PreR enderRecur siveIntern al() +148
System.Web.UI.Control.PreR enderRecur siveIntern al() +233
System.Web.UI.Control.PreR enderRecur siveIntern al() +233
System.Web.UI.Control.PreR enderRecur siveIntern al() +233
System.Web.UI.Control.PreR enderRecur siveIntern al() +233
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +4437
Object reference not set to an instance of an object.
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls.
System.Web.UI.Control.Init
System.Web.UI.Control.Init
System.Web.UI.Control.Init
System.Web.UI.Control.Adde
System.Web.UI.ControlColle
System.Web.UI.WebControls.
System.Web.UI.ControlColle
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.DataSourceVi
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Page.Process
Is your gridview part of another templated control? Is intellisense recognizing the GridView1 (or whatever your's is called)? In other words.. is it the GridView that wasn't found or is it the Column(index) that wasn't found?
ASKER
The gridview is in a master page but I don't think that makes a difference. The intellisense sees it so I assume it's the column it can't find.
ASKER
The gridview is part of a master page but I don't think that should make a difference. Intellisense sees the grid so I'm assuming it's the column it can't find.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Went with this:
Protected Sub gvNBIDs_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. GridViewCo mmandEvent Args) Handles gvNBIDs.RowCommand
If e.CommandName = "Edit" Then
gvNBIDs.Columns(5).Visible = True
ElseIf e.CommandName = "Cancel" Then
gvNBIDs.Columns(5).Visible = False
End If
End Sub
And it works. Thanks for the suggestions.
Protected Sub gvNBIDs_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.
If e.CommandName = "Edit" Then
gvNBIDs.Columns(5).Visible
ElseIf e.CommandName = "Cancel" Then
gvNBIDs.Columns(5).Visible
End If
End Sub
And it works. Thanks for the suggestions.