Here are few coding snippets for our customers regarding C1Olap for LightSwitch.
If you want a default view to be loaded manually using code, you should add the required view definition file to the project and use the following steps :
VB.NET
Private Sub AnalyzeProducts_Created()
Dim _olapPageProxy As IContentItemProxy
_olapPageProxy = Me.FindControl("C1OlapPage")
AddHandler \_olapPageProxy.ControlAvailable, AddressOf pageProxy\_ControlAvailable
End Sub
Private Sub pageProxy_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)
Dim _olapPage = CType(e.Control, C1OlapPage)
If _olapPage IsNot Nothing Then
Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(
Sub()
Dim assembly = Reflection.Assembly.GetExecutingAssembly()
Dim stream As StreamReader = New StreamReader(assembly.GetManifestResourceStream("LoadViewFile.olapx")) ' set the file name here
_olapPage.ViewDefinition = stream.ReadToEnd()
End Sub)
End If
End Sub
C#.NET
partial void AnalyzeProducts_Created()
{
IContentItemProxy _olapPageProxy =this. FindControl("C1OlapPage");
\_olapPageProxy.ControlAvailable +=\_olapPageProxy_ControlAvailable;
}
void \_olapPageProxy\_ControlAvailable(object sender, ControlAvailableEventArgs e)
{
var _olappage = e.Control as C1OlapPage;
if(_olappage != null)
{
Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke( () =>
{
var assembly = Reflection.Assembly.GetExecutingAssembly();
StreamReader stream = new StreamReader(assembly.GetManifestResourceStream("LoadViewFile.olapx")); // set the file name here
_olapPage.ViewDefinition = stream.ReadToEnd();
});
}
}
The default tab shown in C1OlapPanel is OlapGrid. If you wish to change the selected tab order, say select OlapChart, then use the following code : VB.Net
Private Sub AnalyzeProducts_Created()
Dim _olapPageProxy As IContentItemProxy
_olapPageProxy = Me.FindControl("C1OlapPage")
AddHandler \_olapPageProxy.ControlAvailable, AddressOf pageProxy\_ControlAvailable
End Sub
Private Sub pageProxy_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)
Dim _olapPage = CType(e.Control, C1OlapPage)
If _olapPage IsNot Nothing Then
Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(
Sub()
Dim parent As TabItem = TryCast(_olapPage.OlapGrid.Parent, TabItem)
If parent IsNot Nothing Then
Dim tabs As TabControl = TryCast(parent.Parent, TabControl)
tabs.SelectedIndex = 1
End If
End Sub)
End If
End Sub
C#.NET
partial void AnalyzeProducts_Created()
{
IContentItemProxy _olapPageProxy = this. FindControl("C1OlapPage");
\_olapPageProxy.ControlAvailable +=\_olapPageProxy_ControlAvailable;
}
void \_olapPageProxy\_ControlAvailable(object sender, ControlAvailableEventArgs e)
{
var _olappage = e.Control as C1OlapPage;
if(_olappage != null)
{
Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke( () =>
{
TabItem parent = (TabItem)(_olappage.OlapGrid.Parent);
if(parent !=null)
{
TabControl tabs = (TabControl) parent.Parent;
tabs.SelectedIndex = 1;
}
});
}
}
Following code can be used to sort the columns in the Value Fields section in the OlapGrid. Please note that this will work only when there are fields added in the ValueFields section. VB.NET
Private Sub AnalyzeProducts_Created()
Dim _olapPageProxy As IContentItemProxy
_olapPageProxy = Me.FindControl("C1OlapPage")
AddHandler \_olapPageProxy.ControlAvailable, AddressOf pageProxy\_ControlAvailable
End Sub
Dim _olapPage as C1OlapPage
Private Sub pageProxy_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)
Dim _olapPage = CType(e.Control, C1OlapPage)
End Sub
Private Sub Products_Loaded(succeeded As Boolean)
If succeeded = True Then
Dispatchers.Main.BeginInvoke(
Sub()
Dim olap = _olapPage.OlapEngine
olap.BeginUpdate()
olap.RowFields.Add("ProductName")
olap.RowFields.Add("ProductID")
olap.ValueFields.Add("UnitPrice")
olap.EndUpdate()
\_olapPage.OlapEngine.OlapDefaultView.SortDescriptions.Add(New System.ComponentModel.SortDescription(\_olapPage.OlapEngine.OlapTable.Columns(0).ColumnEscapedName, System.ComponentModel.ListSortDirection.Descending))
End Sub)
End If
End Sub
C#.NET
C1OlapPage _olappage;
partial void AnalyzeProducts_Created()
{
Dim _olapPageProxy As IContentItemProxy
IContentItemProxy _olapPageProxy =this. FindControl("C1OlapPage");
_olapPageProxy.ControlAvailable += (s, ea) =>
{
_olappage = ea.Control as C1OlapPage;
};
}
partial void Products_Loaded(bool succeeded)
{
if (succeeded)
{
Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
{
var olap = _olappage.OlapEngine;
olap.BeginUpdate();
olap.RowFields.Add("ProductName");
olap.RowFields.Add("ProductID");
olap.ValueFields.Add("UnitPrice");
olap.EndUpdate();
\_olappage.OlapEngine.OlapDefaultView.SortDescriptions.Add(new System.ComponentModel.SortDescription(\_olappage.OlapEngine.OlapTable.Columns[0].ColumnEscapedName, System.ComponentModel.ListSortDirection.Descending));
});
}
}
Few customers want to just load the default view and does not want the user to edit the loaded view. For this, you can hide the OlapPanel using the following code. VB.NET
Private Sub AnalyzeProducts_Created()
Dim _olapPageProxy As IContentItemProxy
_olapPageProxy = Me.FindControl("C1OlapPage")
AddHandler \_olapPageProxy.ControlAvailable, AddressOf pageProxy\_ControlAvailable
End Sub
Private Sub pageProxy_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)
Dim _olapPage = CType(e.Control, C1OlapPage)
If _olapPage IsNot Nothing Then
Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(
Sub()
_olappage.OlapPanel.Visibility = System.Windows.Visibility.Collapsed
End Sub)
End If
End Sub
C#.NET
partial void AnalyzeProducts_Created()
{
Dim _olapPageProxy As IContentItemProxy
IContentItemProxy _olapPageProxy =this. FindControl("C1OlapPage");
_olapPageProxy.ControlAvailable += (s, ea) =>
{
_olappage = ea.Control as C1OlapPage;
_olappage.OlapPanel.Visibility = System.Windows.Visibility.Collapsed;
};
}