[Last Call] Learn how to a build a cloud-first strategyRegister Now


GridView EditTemplate

Posted on 2007-10-18
Medium Priority
Last Modified: 2013-11-07
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,

Question by:stretch73
  • 5
  • 4
LVL 16

Expert Comment

ID: 20101385
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.

Author Comment

ID: 20101449
Here's what I have:

                        <asp:TemplateField ItemStyle-HorizontalAlign="center" HeaderText="NBID">
                                <asp:Label ID="lblStandardID" Text='<%# Eval("Standard_ID") %>' runat="server" Visible="false" />&nbsp;
                                <asp:TextBox ID="txtNBID" MaxLength="7" Width="50px" Text='<%# Bind("Standard_ID") %>' runat="server" /><br />


but I can still see the column.
LVL 16

Expert Comment

ID: 20101649
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
            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.
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


Author Comment

ID: 20101747
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
LVL 16

Expert Comment

ID: 20101927
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?

Author Comment

ID: 20102096
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.  

Author Comment

ID: 20102206
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.
LVL 16

Accepted Solution

TSmooth earned 2000 total points
ID: 20130344
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.

Author Comment

ID: 20131548
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.

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question