Answered by:
Programatically changing the label/display name of an editable grid

Question
-
I have an editable grid with multiple rows of data and I want to programatically change the heading or display name of a few columns. I have code of:
this.FindControlInCollection("HistYr1Value", this.SomeQuery.ElementAt(0)).DisplayName = HistYr1;
where HistYr1 is a string value that is calculated as one year prior to the current date.
However, this code doesn't change the display name for the HistYr1Value column heading of the editable grid but the code does change it for the 1st (ie, 0th) element in the collection array (via debug watch in VS). Any idea on how to change the column heading of an editable grid?
Friday, November 25, 2011 8:18 AM
Answers
-
Hi,
I recently write about this and here is againg if you would like to change grid column header display name at runtime:
partial void CustomersListDetail_Created() { this.FindControl("VisitsCollection").ControlAvailable += ChangeHeader; } private void ChangeHeader(Object sender, ControlAvailableEventArgs args) { DataGrid myGrid; myGrid = args.Control as DataGrid; DataGridColumn column = (DataGridColumn) myGrid.Columns.Where(a => a.Header.ToString() == "Item").FirstOrDefault(); if (null!= column) { ContentItemWrapperForColumnHeader headerFeature = (ContentItemWrapperForColumnHeader)column.Header; headerFeature.ContentItem.DisplayName = "NewColumnName"; } }
Remarks:
VisitsCollection - name of the collection in Detail part of the screen
Item - Name of the header column in DataGrid
NewColumnName - your new column name for column Item
Prerequisites:
Need to Add References to Client project (File View/Client/References/Add References) to:
1. System.Windows.Controls.Data (.NET tab of Add Reference dialog)
2. Microsoft.LightSwitch.Client.Internal (Use Browse tab do point do dll: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\LightSwitch\1.0\Client\Microsoft.LightSwitch.Client.Internal.dll)
System.Windows.Controls.Data is for DataGrid objects
Microsoft.LightSwitch.Client.Internal.dll is for ContentItemWrapperForColumnHeader
Hope this help
Spaso Lazarevic- Edited by Spaso Lazarevic Friday, November 25, 2011 8:39 AM
- Proposed as answer by Simon Jones [MSDL] Friday, November 25, 2011 9:51 AM
- Marked as answer by stratus80 Friday, November 25, 2011 7:18 PM
Friday, November 25, 2011 8:38 AM
All replies
-
Hi,
I recently write about this and here is againg if you would like to change grid column header display name at runtime:
partial void CustomersListDetail_Created() { this.FindControl("VisitsCollection").ControlAvailable += ChangeHeader; } private void ChangeHeader(Object sender, ControlAvailableEventArgs args) { DataGrid myGrid; myGrid = args.Control as DataGrid; DataGridColumn column = (DataGridColumn) myGrid.Columns.Where(a => a.Header.ToString() == "Item").FirstOrDefault(); if (null!= column) { ContentItemWrapperForColumnHeader headerFeature = (ContentItemWrapperForColumnHeader)column.Header; headerFeature.ContentItem.DisplayName = "NewColumnName"; } }
Remarks:
VisitsCollection - name of the collection in Detail part of the screen
Item - Name of the header column in DataGrid
NewColumnName - your new column name for column Item
Prerequisites:
Need to Add References to Client project (File View/Client/References/Add References) to:
1. System.Windows.Controls.Data (.NET tab of Add Reference dialog)
2. Microsoft.LightSwitch.Client.Internal (Use Browse tab do point do dll: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\LightSwitch\1.0\Client\Microsoft.LightSwitch.Client.Internal.dll)
System.Windows.Controls.Data is for DataGrid objects
Microsoft.LightSwitch.Client.Internal.dll is for ContentItemWrapperForColumnHeader
Hope this help
Spaso Lazarevic- Edited by Spaso Lazarevic Friday, November 25, 2011 8:39 AM
- Proposed as answer by Simon Jones [MSDL] Friday, November 25, 2011 9:51 AM
- Marked as answer by stratus80 Friday, November 25, 2011 7:18 PM
Friday, November 25, 2011 8:38 AM -
Thanks Spaso. This fixed the issue - I had to do a debug watch on the grid control to find the right name (it is the display name in the LS dev tool property) and the references had to be tweaked (added Microsoft.Lightswitch.Presentation.Implementation.Controls and System.Windows.Controls as the references needed for DataGrid and ContentItemWrapperFor ColumnHeader).
Thanks for the help on this - greatly appreciated!
Friday, November 25, 2011 7:26 PM -
BTW- I now need to do this with a layout of individual Text Box controls. How do I change the label programatically on a text box? I used the "this.FindControl()" to find the text box label I want to change, but I can't seem to find the property or method to change the display name of that specific text box. Any ideas?Saturday, December 17, 2011 7:54 AM
-
Figured this out.. for a simple text box, it is this.FindControl("control name").DisplayName = someStringValue;Saturday, December 17, 2011 8:10 AM