• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 841
  • Last Modified:

Hide columns when editing GridView

I am having problem controlling the visibility of the columns in a gridview. My gridview needs to change columns according to what state it is in, like:
ItemTemplate: column A, B, C, D, E
EditItemTemplate: column A, C, D, F, G

The columsn are equivalent to column[indexNumber], like A = column[1].

How can I achieve it?
I have tried by adding the following to the OnRowDataBound:
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (GridView1.EditIndex < 0)
        {
           //this is in edit mode, and need to hide:
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[5].Visible = false;
            return;
        }

        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) &&
           (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //this is when viewing ItempTemplate, and need to hide:
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
        }
    }

All columns have their visibility set to true.
The code above actually works on hiding columns when in ItemTemplate, but it has no effect on the editMode so I suspect something is wrong with timing or perhaps my first if...although it seems fine when I output gridView.EditIndex...

Really need help with this tiny problem...
Thanks!
0
wahooo
Asked:
wahooo
1 Solution
 
LimbeckCommented:
i havent tested your code, but Editindex >=0 means the gridview is in edit mode.

from the helpfiles (VS2005):

Property Value
The zero-based index of the row to edit. The default is -1, which indicates that no row is being edited.

so try this instead:
     if (GridView1.EditIndex >= 0)
0
 
wahoooAuthor Commented:
I just found the solution, only needed some thinking...
Here's the solution for everyone who is struggling with the same problem:
----aspx.cs----
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (GridView1.EditIndex < 0)
        {
            if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) &&
                (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
            {
                //this is itemTemplate-mode and you set the visibility accordingly
                e.Row.Cells[3].Visible = true;
                e.Row.Cells[4].Visible = true;
                e.Row.Cells[5].Visible = true;
                e.Row.Cells[6].Visible = false;
            }
        }
        else if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            //this is edit-mode and you set the visibility accordingly
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = true;
        }
    }
-----

My problem occured due to the fact that the GridViewRowEventArgs included more cells than what I expected, hence giving me error messages when I tried to alter e.Row.Cells.etc without testing that it was a true cell.

Also, the OnRowDataBound is run for every row, meaning that when I tested:
if(e.Row.RowState == DataControlRowState.Edit)
I only got the wanted changes for the row I was actually editiing, as the rest of the rows still was in "normal" state. Initially I thought it checked whole columns, not cell by cell in row by row...

Hope this helps someone else.
0
 
royendCommented:
Dear wahooo.
I actually believe this was my idea posted to you in an email?

Hopefully you will confirm this by accepting "this post" as the solution...
Thanks!
0
 
chrisgreavesCommented:
THANKS GUYS! YOU SAVED ME A LOT OF TIME!
CHRIS
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.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now