Need to hide columns in a DevExpress xtraGrid - ver 6.3

Need to hide columns in a DevExpress xtraGrid - ver 6.3

Old forum URL: forums.lhotka.net/forums/t/4097.aspx


Q Johnson posted on Friday, December 28, 2007

Sure 6.3 is old school - but this is an old VS2003 app using CSLA 1.53.

 

I've got a collection (generic) of CSLA Editable root objects that have four collection members.  I set the Browseable attribute False wherever I can, but can't find a way to do that for .IsNew, .IsDeleted, and .BrokenRulesString. 

So I need to hide these columns after setting my generic collection as the DataSource.  The Master/Detail grid is really awesome and it allows all the editing I need.  But I really want to hide these silly looking properties.  Actually I don't even need them because after any changes made by the user, the object graph is getting written to a csv file - not persisted back to the database from which it was extracted.

I know I want to do something in my Form's load event to iterate the grid's various views (one for master view of the EROs and one each for the four collections) and itereate each's columns collection looking for matches by caption or FieldName and set the visible property to false for them.  But I have scoured tutorials and documentation and left msgs on the DevEx forum and am still unable to find the syntax to do this.

I'm trying

With xgrdDisplayData

   .DataSource = mcolWarrants

   .Refresh()

'TODO: Figure out what class name to use for the Column dude

   For Each oCol As DevExpress.XtraGrid.Columns.GridColumn In xgrdDisplayData.Columns

      If oCol.FieldName = "IsNew" _

      OrElse oCol.FieldName = "IsDeleted" _

      OrElse oCol.FieldName = "BrokenRuleString" Then

         oCol.Visible = False

      End If

   Next

End With

but, of course, there is no such thing as xgrdDisplayData.Columns (if only it were that easy!!!).

Thanks in advance.

Q Johnson replied on Tuesday, January 01, 2008

I finally found my solution.  In case any of you have to do this, I'll offer what I finally found after what I think was WAY too long a time studying the docs.
 
Requirement:
-------------
I wanted to hide the same three columns (whose names I knew) in both the MasterView and the four detail views related to it.
 
For the MasterView:
-------------------
When I dropped the grid control on my form, I didn't even notice that DevExpress named the MainView object "GridView1" for me.  I spent a lot of wasted time trying to learn the syntax for declaring some object that had the columns collection I wanted and was actually successful with that technique; but it is far easier to just use the one provided (duh) and put code like this in your form's load event:
 

For Each oCol As DevExpress.XtraGrid.Columns.GridColumn In GridView1.Columns

        If oCol.FieldName = "IsNew" _

        OrElse oCol.FieldName = "IsDeleted" _

        OrElse oCol.FieldName = "BrokenRulesString" Then

                oCol.Visible = False

        End If

Next

I knew it would be simple once I discovered syntax for that object with the columns collection.  But this is REALLY simple, eh?  A little more work was required for the detail views.
 
 
For the DetailViews:
--------------------
My study of the docs taught me that there is a PatternView that is a Template for the detail view that will be shown when the user expands the MasterRow and causes a particular Detail View to be shown.  That detail view is just a very temporary CLONE of the PatternView that will disappear when the MasterRow is collapsed.  So to make my columns invisible, I just needed to trap the event that shows the Detail view and place my code there.  The choice of event is important.  In fact, so is the control.  We need the event on the VIEW, so I had to look at GridView1.  Yours will vary by how you name it, of course.  There are two that look appealing on the GridView's list: MasterRowExpandING and MasterRowExpandED (caps for emphasis are mine, of course).  It turned out that MasterRowExpandING was too soon; the Detail View wasn't available yet via the GetDetailView method.  But the MasterRowExpandED was perfect.  Here's all the code I needed there: 
 

Private Sub GridView1_MasterRowExpanded(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs) Handles GridView1.MasterRowExpanded

Dim oPatternView As DevExpress.XtraGrid.Views.Grid.GridView

oPatternView = CType(GridView1.GetDetailView(e.RowHandle, _ e.RelationIndex), DevExpress.XtraGrid.Views.Grid.GridView)

For Each oCol As DevExpress.XtraGrid.Columns.GridColumn In _ oPatternView.Columns

    If oCol.FieldName = "IsNew" _

    OrElse oCol.FieldName = "IsDeleted" _

    OrElse oCol.FieldName = "BrokenRulesString" Then

        oCol.Visible = False

    End If

Next

oPatternView = Nothing

End Sub

 
Note how friendly the event arguments are: e.RowHandle and e.RelationIndex were exactly what I needed to get my detail view.  In my case, I was making exactly the same changes to columns of the same name in each detail view.  I think this is probably an unusual case and you are likely to want to look at the value for e.RelationIndex (or otherwise determine which DetailView has come into view to fire this event) so that you'll know what changes need to be made.
 
Best of all, in my quick look into the 7.3 docs, it looks as if the same object model is used, so this information should be useful to anyone who wrestles with this issue there, too.
 
Once you know the syntax, it's really quite impressive what you can do with just a little code with this control - even in the bad old days of .NET 1.1, eh?
 
Happy New Year!

malkav replied on Thursday, April 01, 2010

Thank you so much! i was trapped in the same problem for 2 days!!

Copyright (c) Marimer LLC