weweber3
weweber3

Reputation: 31

Lightswitch - Hiding Columns

FindControl seems to only reference the name of the grid, not the column name inside the grid. I can't find any documentation or examples regarding FindControlInCollection either.

I don't have any sophisticated logic to wrap this in at this point. Just need to hide columns. I am using C# and VS Update 2.

Upvotes: 3

Views: 1908

Answers (2)

Yann Duran
Yann Duran

Reputation: 3879

Bryan's answer contains what you need.

FindControl only gets a proxy for the control. While there are a few things you can set using it, the only way to get to the actual control is to access it through the proxy's ControlAvailable handler, which provides a reference to the underlying control in its ControlAvailableEventArgs parameter.

Also, as you can see, you don't actually set column visibility via the controls that are used in the grid, you set it using the DataGrid's Columns collection directly instead.

Upvotes: 2

Bryan Hong
Bryan Hong

Reputation: 1483

Under the Activate event of the screen, use this codeblock:

  1. Get an IControlItemProxy using the name of the grid.
  2. Get the control itself.
  3. Access the column by its index and set its visibility dynamically.
  4. Add a using directive to System.Windows.Controls.

.

partial void ScreenName_Activated()
{
    IContentItemProxy proxy = this.FindControl("NameOfGrid");

    proxy.ControlAvailable += new EventHandler<ControlAvailableEventArgs>((s1, e1) =>
        {
            DataGrid dataGrid = (DataGrid)e1.Control;

            dataGrid.Columns[0].Visibility = System.Windows.Visibility.Collapsed;
            dataGrid.Columns[1].Visibility = System.Windows.Visibility.Collapsed;
        });
}

Upvotes: 2

Related Questions