none
How to: Dynamically Show/Hide DataGrid Columns

    Question

  • Hi there!

    Is there a way to dynamically Show/Hide DataGrid Columns?

    Many thanks for any kind of help.

    Regards,


    Ciro
    Friday, August 19, 2011 11:31 AM

Answers

All replies

  • You'd have to get a reference to the grid control, cast it to a Silverlight Grid, then you can do anything that you can do with a SL control.


    Yann

    (plus ça change, plus c'est la même chose!)

    Friday, August 19, 2011 12:57 PM
    Moderator
  • Hi Yann! thanks to reply!

    Is it possible to give an example. I don't know nothing about Silverlight.

    Many thanks for any kind of help.

    Regards,


    Ciro
    Friday, August 19, 2011 1:02 PM
  • DataGrid Class

    Scroll down to the "Columns" section.


    Friday, August 19, 2011 3:26 PM
  • Andy, thanks to replay!

    I tried to get reference to the grid:

    Private Sub MyScreen_InitializeDataWorkspace(........)

    Dim ctr = CType(FindControl("Grid"), DataGrid)

    End Sub

    But I got the following error message:

    "Unable to cast object of type 'Microsoft.LightSwitch.Presentation.Implementation.ContentItemProxy' to type 'System.Windows.Controls.DataGrid'."

    Any suggestion?

    Best regards,

    Ciro.


    Ciro
    Friday, August 19, 2011 6:02 PM
  • Ciro,

    You have to access the grid through code just like every other control in LightSwitch, that is to say, on it's "ControlAvailable" Event.  Add an event handler for FindControl("Grid").ControlAvailable, then cast your e.Control as a DataGrid.

    Hope that helps.

    Andy

    • Proposed as answer by Chris.Hart Tuesday, September 13, 2011 3:34 PM
    Friday, August 19, 2011 6:13 PM
  • Andy,

    Where can put a code like this (I got the following error message: Invalid cross-thread access.):

    Private Sub ShowHideColumn_Execute()
          ' 
          If _dataGrid.Columns(0).Visibility = Windows.Visibility.Collapsed Then
            _dataGrid.Columns(0).Visibility = Windows.Visibility.Visible
          Else
            _dataGrid.Columns(0).Visibility = Windows.Visibility.Collapsed
          End If
        End Sub
    

    Best regards,


    Ciro
    Friday, August 19, 2011 6:49 PM
  • Andy, thanks a lot!

    For everybody who wants to know How to Hide/Show DataGrid Column On Fly:

    Imports Microsoft.LightSwitch.Threading
    Imports System.Windows.Controls
    
    Namespace LightSwitchApplication
    
      Public Class SearchCarroes
    
        Private WithEvents _dataGrid As DataGrid
    
        Private Sub SearchCarroes_InitializeDataWorkspace(saveChangesTo As System.Collections.Generic.List(Of Microsoft.LightSwitch.IDataService))
          ' Write your code here.
          AddHandler FindControl("Grid").ControlAvailable, AddressOf CONTROL_AVAILABLE
    
        End Sub
    
        Private Sub CONTROL_AVAILABLE(sender As Object, e As ControlAvailableEventArgs)
    
          If TypeOf (e.Control) Is DataGrid Then
            _dataGrid = CType(e.Control, DataGrid)
            '
            _dataGrid.Columns(0).Visibility = Windows.Visibility.Collapsed
          End If
        End Sub
    
        Private Sub ShowHideColumn_Execute()
          ' 
          Dispatchers.Main.Invoke(Sub()
                        If _dataGrid.Columns(0).Visibility = Windows.Visibility.Collapsed Then
                          _dataGrid.Columns(0).Visibility = Windows.Visibility.Visible
                        Else
                          _dataGrid.Columns(0).Visibility = Windows.Visibility.Collapsed
                        End If
                      End Sub)
        End Sub
      End Class
    
    End Namespace
    


    If you find this information useful, please vote!

    Regards,

     


    Ciro
    Friday, August 19, 2011 7:32 PM
  • Hi Ciro,

    Thanks for your example.  I was expecting a very simple implementation of your example, but I came across a little problem.  For the line below, VS is complaining that DataGrid is not defined.  I have placed the "Imports System.Windows.Controls" at the top and thought that should solve it.

    Private WithEvents _dataGrid As DataGrid
    

    Do you have any pointers for me?

    Many thanks..

     

    Best regards,

    CT

    Tuesday, December 27, 2011 5:09 AM
  • If you look, you'll see that the "Imports System.Windows.Controls" was already there.

    What you need is an assembly reference, "System.Windows.Controls.Data", added to the Client project.

    Although the namespace is "System.Windows.Controls", the DataGrid isn't available, as you've discovered, without the reference to the "System.Windows.Controls.Data" assembly.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.
    Tuesday, December 27, 2011 6:48 AM
    Moderator
  • I tried to convert this to VB.NET but all the converters fail.  Do you have a VB.NET equivalent?

    Thanks!

    Sunday, October 14, 2012 3:37 AM
  • Sunday, October 14, 2012 7:40 AM
  • Andy - you'll make your replies a lot easier to read if you use the "link" button on the reply toolbar.

    That way we can see text that tells us what the link is about, but can click the link to be taken to the web page.

    So your reply would look like this instead:

    Silverlight ChildWindow - UnauthorizedAccessException - Invalid cross-thread access.

    Invalid Cross-thread access... bug?

    Invalid cross-thread access. RSS

    Much easier to read, yes?


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Tuesday, October 16, 2012 4:25 AM
    Moderator
  • Maybe the problem is that the code is already VB.NET code?

    If not, which code are you referring to?


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Tuesday, October 16, 2012 4:27 AM
    Moderator