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  
stretch73Asked:
Who is Participating?
 
TSmoothCommented:
Are you familiar with using the debugger in Visual studio? If so, use it to check on the count of the columns and to see if the columns exist at that point. One thing I just noticed is that from your error output, it appears you put the code I suggested in the controls Init event and I think that might be the problem. The columns aren't yet created. I believe you need to put the code in the RowDataBound event as I suggested.
0
 
TSmoothCommented:
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.
0
 
stretch73Author Commented:
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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
TSmoothCommented:
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.
0
 
stretch73Author Commented:
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
0
 
TSmoothCommented:
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?
0
 
stretch73Author Commented:
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.  
0
 
stretch73Author Commented:
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.
0
 
stretch73Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.