Articles → LIGHT SWITCH 2011 → Datagrid Header Text Wrapping In Lightswitch 2011

Datagrid Header Text Wrapping In Lightswitch 2011






Software Requirement




  1. Visual studio 2010 is installed on your machine.
  2. Visual Studio 2010 service pack 1 is installed on your machine.
  3. Microsoft Visual Studio Light switch 2011 is installed on your machine.

Prerequisite Knowledge




  1. How to create lightswitch project?
  2. How to create an entity in lightswitch?
  3. How to create a screen in lightswitch?
  4. What is the ControlAvailable event in lightswitch?
  5. Basics about Silverlight.
  6. How to create DataTemplate in Silverlight?

Steps For Execution




  1. Create a new lightswitch project
  2. Create a new table
  3. Add new screen
  4. Add data in the table
  5. Add ControlAvailable event for the grid
  6. Add code for column wrap



Create A New Lightswitch Project




Picture showing the project structure of the lightswitch application in visual studio

Click to Enlarge


Create A New Table




Picture showing the sample table in the lightswitch designer

Click to Enlarge




Add New Screen




Picture showing the editable grid screen in the designer window

Click to Enlarge


Add Data In The Table




Picture showing the datagrid with data in the editable grid screen

Click to Enlarge




Add Controlavailable Event For The Grid




partial void EditableEmployeesGrid_Created() {
  this.FindControl("grid").ControlAvailable += new EventHandler < ControlAvailableEventArgs > (EditableEmployeesGrid_ControlAvailable);
}

void EditableEmployeesGrid_ControlAvailable(object sender, ControlAvailableEventArgs e) {

}



Add Code For Column Wrap


partial void EditableEmployeesGrid_Created() {
  this.FindControl("grid").ControlAvailable += new EventHandler <ControlAvailableEventArgs> (EditableEmployeesGrid_ControlAvailable);
}

DataGrid _grid;
void EditableEmployeesGrid_ControlAvailable(object sender, ControlAvailableEventArgs e) {
  if (e.Control is DataGrid) {
    _grid = (DataGrid) e.Control;
    StringBuilder sb = new StringBuilder();

    sb.Append(@ "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>");
    sb.Append(@ "<TextBlock TextWrapping='Wrap'  Text='{Binding}'/>");
    sb.Append(@ "</DataTemplate>");

    Style newGridHeaderStyle = new Style(typeof (DataGridColumnHeader));

    newGridHeaderStyle.Setters.Add(new Setter {
      Property = DataGridColumnHeader.ContentTemplateProperty, Value = (DataTemplate) XamlReader.Load(sb.ToString())
    });

    _grid.ColumnHeaderStyle = newGridHeaderStyle;
  }
}





Output


Picture showing the output of the Datagrid header text wrapping in lightswitch 2011

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, December 5, 2012

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250