Link to home
Start Free TrialLog in
Avatar of stretch73
stretch73Flag for United States of America

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  
Avatar of TSmooth
TSmooth

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.
Avatar of stretch73

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" />&nbsp;
                            </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).Visible = True
        Else
            GridView1.Columns(0).Visible = 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.
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.BaseValidator.OnInit(EventArgs e) +41
   System.Web.UI.Control.InitRecursive(Control namingContainer) +459
   System.Web.UI.Control.InitRecursive(Control namingContainer) +271
   System.Web.UI.Control.InitRecursive(Control namingContainer) +271
   System.Web.UI.Control.AddedControl(Control control, Int32 index) +327
   System.Web.UI.ControlCollection.Add(Control child) +263
   System.Web.UI.WebControls.RowControlCollection.Add(Control child) +55
   System.Web.UI.ControlCollection.AddAt(Int32 index, Control child) +44
   System.Web.UI.WebControls.RowControlCollection.AddAt(Int32 index, Control child) +60
   System.Web.UI.WebControls.TableRowCollection.AddAt(Int32 index, TableRow row) +48
   System.Web.UI.WebControls.TableRowCollection.Add(TableRow row) +32
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +179
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +4305
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +88
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +38
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +126
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +98
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +153
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
   System.Web.UI.WebControls.GridView.DataBind() +23
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +92
   System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +33
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +74
   System.Web.UI.Control.PreRenderRecursiveInternal() +148
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4437
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?
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.  
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
Avatar of TSmooth
TSmooth

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Went with this:

    Protected Sub gvNBIDs_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) 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.