Articles → LIGHT SWITCH 2011 → Show And Hide Grid Columns At Runtime In Lightswitch 2011
Show And Hide Grid Columns At Runtime In Lightswitch 2011
Software Requirement
- Visual studio 2010 is installed on your machine.
- Visual Studio 2010 service pack 1 is installed on your machine.
- Microsoft Visual Studio Light switch 2011 is installed on your machine.
Prerequisite Knowledge
- What is lightswitch?
- What are screens and entities in lightswitch?
- How to create an entity in lightswitch?
- How to create a screen in lightswitch?
Creation Of Employee Entity
- FirstName
- LastName
- Address
Click to Enlarge
Creation Of An Editable Grid
Click to Enlarge
Adding Data In The Grid
Click to Enlarge
Add Buttons For Show And Hide
- Double click on the screen EditableEmployeeGrid inside the Screen folder in Solution Explorer.
- On the designer screen click on the Screen Command Bar.
- On click of it Add button becomes visible.
- Click on the arrow sign on the Add button.
- A context menu appears.
- Click on New Button… option.
Click to Enlarge
Click to Enlarge
Click to Enlarge
- Click (single click) on the button.
- Press F4.
- Property window will open on right hand side.
- Change the Display Name and save.
Click to Enlarge
Add A Dll
- Click on the project and click on the arrow as shown in figure below.
Click to Enlarge
- A context menu appears click on the file view
Click to Enlarge
- Once you click on file view the project view changes as shown in figure below
Click to Enlarge
Click to Enlarge
Code For Showing The Grid Column
- Right click on the button
- Click Edit Execute Code.
Click to Enlarge
private DataGrid _dataGrid;
partial void Command_Show_Execute() {
// Write your code here.
this.FindControl("grid").ControlAvailable += ShowGrid;
}
private void ShowGrid(object sender, ControlAvailableEventArgs e) {
if (e.Control is DataGrid) {
_dataGrid = (DataGrid) e.Control;
_dataGrid.Columns[2].Visibility = System.Windows.Visibility.Visible;
}
}
Code For Hiding The Grid Column
- Right click on the button
- Click Edit Execute Code.
- Add the following code in the execute method.
partial void Command_Hide_Execute() {
// Write your code here.
this.FindControl("grid").ControlAvailable += HideGrid;
}
private void HideGrid(object sender, ControlAvailableEventArgs e) {
if (e.Control is DataGrid) {
_dataGrid = (DataGrid) e.Control;
_dataGrid.Columns[2].Visibility = System.Windows.Visibility.Collapsed;
}
}
Output
Click to Enlarge
Click to Enlarge
Click to Enlarge