How to hide a Column in a gridview, Visual Web Developer

Hello All,

I have a Web Application I am developing that has a GridView.
I have searched the internet to no avail looking for an answer to this.

The gridview has 11 columns. I want to hide the 11th column while in normal mode and make visible while in edit mode.

Here is my Code:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowState = DataControlRowState.Edit Then
GridView1.Columns(11).Visible = True

ElseIf e.Row.RowState = DataControlRowState.Normal Then
GridView1.Columns(11).Visible = False

End If
End Sub


My result from the above code is The 11th column is not visible in Normal mode but when I click the Edit button an error StackOverflowExceptionwasunhandled comes up.

What am I missing?

Comments

  • seancampbellseancampbell Pennsylvania, USA
    FYI, this is a VB.Net forum here, there is a seperate ASP.Net forum where you can go to ask web questions... Since I am too lazy to reassign this to anothre forum... here's your answer:


    RowDataBound is called for each row.
    Only ONE row will have the edit state set, and the rest will be normal.
    Switching columns to Visible or Not Visible is not row specific, in other words you cannot just show a column for one row and not for the others.
    Therefore, the last row to call "RowDataBound" has the "lasting" effect of whether that column is visible or not.

    What I normally do:
    If I have an "Actions" column that allows me to to perform actions such as "Save, Edit, Delete, Open" I will add all of the controls that I want there WITH VISIBLE SET TO FALSE... On RowDataBound I check the state of the row and get the control's from that row and set them to Visible = True. Like this:

    [code]
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    If e.Row.RowState = DataControlRowState.Edit Then
    Dim saveBtn As ImageButton
    saveBtn = e.Row.FindControl("btnGridViewSave")
    saveBtn .Visible = True

    Dim cancelBtn As ImageButton
    cancelBtn = e.Row.FindControl("btnGridViewCancel")
    cancelBtn .Visible = True
    ElseIf e.Row.RowState = DataControlRowState.Normal Then
    Dim deleteBtn As ImageButton
    deleteBtn = e.Row.FindControl("btnGridViewDelete")
    deleteBtn.Visible = True
    End If

    End Sub
    [/code]

    In this example I have 3 controls inside the rows, btnGridViewDelete, btnGridViewSave, and btnGridViewEdit that have Visible = False. Whenever data binds to the control, it checks each row's state to see if it is in edit mode and sets the Edit Save And Cancel button's Visibility accordingly.

    Hope this helps,
    Sean C
  • Hello All,

    Well I did try your suggestions howerver nothing worked. I think next time I will manually create the Table and writ ethe code rathe rthan use the Gridview for this tyoe of situation. With that said here is what i did.

    1. Convert the column to a template
    2. The columns that I want to hide - site the width to 0
    3. In the ItemTemplate just remove the text so that the column title is is blank
    4. On the ASP: Code set GridLines = None
    5. In the edit template Put the text/check box you want to use

    Doing the above steps allowed me to hide the columns in "Normal mode" and have them show in edit mode.

    Thanks guys for all the help,
    Tommy
    Thanks,
    Tommy
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories