How do I get at the code Tree Behind Complex GridView/ListView Controls RRS feed

  • General discussion

  • I've asked this question in different ways in the past and never been able to work out a clear picture. I know clear pictures exist because my ASP.NET 4.5 Book spells out all of these details for ASP.NET. I need the same kind of info found in "Professional ASP.NET 4.5 in C# and VB" chapters 3-7 but for the Silverlight Controls found in VS 2012 LightSwitch Controls.

    I was reading for class today in ASP.NET and it clearly states that ASP.NET 4.5 GridView and ListView are in fact sets of controls with Repeater and Constructing a Tree of controls. I'm going to guess Microsoft used and uses similar methods to create and control Silverlight Controls. I want to access the code behind these kinds of controls for LIGHTSWITCH. Expert360 is brilliant but all I ever get in response is a sample project with no direction on how and where to access the FOLDERS and FILES that would structurally contain these controls and tree builders.

    Basically I would like access to see how they are generated so I can think up a way to use conditional code to effect Properties like BorderColor, BorderWidth, BackColor to name a few Properties/Methods specifically that I would like to change conditionally based on the Text String within that part of the controls Tree or other strings from other parts of the Tree or other controls Values altogether. I know in ASP.NET it is saying that the ID's of the whole control are Dynamically Created so I would need to know how those ID's are generated as the control Adds parts or Deletes parts to the tree. That way if I need to read Values or Strings from other parts of the control to write my conditional statements or loops I will have the information to experiment. The Format of my controls often need to communicate VIA Styling and Color beyond what the Value can. That Communications can be at an entity Level or Attribute Level. It would also help to have access to the Events of controls for this kind of sonication information with Post-Back. The Events symbol and windows do not seem to exist in the IDE for LightSwitch but given my rudimentary knowledge of code I do believe I can build basic structures to enhance the code manually if nessisary.

    • Edited by reigh7 Thursday, October 13, 2016 9:51 AM
    Thursday, October 13, 2016 9:50 AM

All replies

  • Perhaps you need a Silverlight book rather than asp.net.

    Dave Baker has undoubtedly the best sample of conditional formatting which is likely the one he's referred you to before here:


    Silverlight uses WPF controls and you set property values with bindings and value converters. For LightSwitch you do this in the screen code something like so:

    • Find the control
    • Add event handler on ControlAvailable
    • In ControlAvailable handler, get the datagrid control and add a handler to its LoadingRows event
    • In the LoadingRows handler set the property (ie:BackColor) binding to a custom IValueConverter

    This method is demonstrated in Dave's sample code above and will allow you to do formatting of any all controls properties on that row.

    Here is a reference to the events of the DataGrid control:


    Best of luck


    • Edited by joshbooker Sunday, October 30, 2016 3:00 PM iPhone can't post
    Sunday, October 30, 2016 3:54 AM
  • I don't know what all that means and just cause some book says something doesn't mean it is accurate anyhow.

    However if you want the code which references numerous classes or interfaces or other information not in the code and sometimes trying to find those classes or interfaces or other information is difficult at best then you can use "Microsoft Reference Source .NET Framework 4.6.2" and this link to that is for the search result GridView.

    GridView is 4605 lines of code not to mention anything it references which has its own code.

    La vida loca

    Sunday, October 30, 2016 4:24 AM
  • Maybe one of the approaches describes in this thread will help?

    Get the instance of a control in a DataGridCell while in LoadingRow method


    Sunday, October 30, 2016 12:05 PM
  • Nice post Paul.  I recall (years ago) referring to that post as Dave's sample doesn't set the formatting of one control based on the value of another which is what that post solves.
    Sunday, October 30, 2016 3:03 PM
  • this looks right I'm not sure how he is using those methods. Did he create a separate class file and put the methods in there to walk down the tree and find the text boxes Inside a column inside of a cell?

    I would like to write the conditional formatting for every Textbox in every cell moving down a specific column. That's assuming the tree is linear and not matrix.

    MonkeyBoy I'm guessing that the controls wrap eachother something like this below but I'm not sure the description in the book was vague and did not tell us how to access the library behind it creating the control. The connection you speak of is probably tied to the Query to SELECT,INSERT, UPDATE, DELETE the relational database of OData. Any-who that's my guess If I could see those 4605 lines that handle all of the validation and all of the if's inserting the right controls and the For Each creating all of the columns For Each selected. And the For Each for the rows after a SELECT SQL query allows it to be searched. The buttons used to add rows or open prompts for data entry or forwarding onto postback or cross postback I should be able to model similar behavior in an ASP.Net for with its grid control and use ASP.Net at my new interface to the OData model replacing my Silverlight screens.


         colRepeater For Each Column in the entity and related entities making up the grid{


                   rowRepeater For Each SELECT STATEMENT * or Filtered{











    I hope to get at the value and write a c# conditional statement like

    if(Value>x && Value<y){

    Cell.Backgroundcolor = Green


    Else If(Value>y && Value<z){

    Cell.BackgroundColor = Yellow


    Else If(Value>z && Value(of course a wrap around but for explaination)<x){

    Cell.BackgroundColor = Pink


    The values are time so the last Else If will have to be two conitionals to account for midnight.

    I also just want to get at the value of the control for a

    This will make it consistently the same color as the Label will define that it is Day for instance is Green.

    This will make it consistently the same color as the Label will define that it is Night for instance is Pink.

    • Edited by reigh7 Monday, October 31, 2016 10:49 PM because Reasons I added
    Monday, October 31, 2016 10:12 PM
  • Here is some of my own code. It starts by adding ControlAvailable event handlers for the lists and grids I want to adjust. These are added in the screen's Created event, and in my case I put the code directly in the screen's code-behind. The CONTROL... parameters are string constants set to the control names as assigned in the screen design view.

    //Save lease list for later use.
    IContentItemProxy leaseList = this.FindControl(CONTROLLeaseList);
    leaseList.ControlAvailable += LeaseList_ControlAvailable;
    //Save invoice list for later use.
    IContentItemProxy invoiceGrid = this.FindControl(CONTROLInvoiceGrid);
    invoiceGrid.ControlAvailable += InvoiceGrid_ControlAvailable;
    //Indicate balance status with text color
    IContentItemProxy leaseBalanceDue = this.FindControl(CONTROLLeaseBalanceDue);
    leaseBalanceDue.ControlAvailable += LeaseBalanceDue_ControlAvailable;
    //Get a reference to the real estate tax autocomplete control.
    IContentItemProxy taxAutoComplete = this.FindControl(CONTROLTaxYear);
    taxAutoComplete.ControlAvailable += TaxAutoComplete_ControlAvailable;
    //Show/hide the lease charge warning
    IContentItemProxy leaseEndDateMismatch = this.FindControl(CONTROLEndDateMismatch);
    leaseEndDateMismatch.ControlAvailable += LeaseEndDateMismatch_ControlAvailable;
    //Indicate balance status with row background color
    IContentItemProxy chargeItemGrid = this.FindControl(CONTROLChargeItemGrid);
    chargeItemGrid.ControlAvailable += ChargeItemGrid_ControlAvailable;

    The ControlAvailable event handlers do the work of retrieving the underlying controls. I use property bindings wherever possible to set the control properties directly to the data. Here are some samples.

    void LeaseList_ControlAvailable(object sender, ControlAvailableEventArgs e)
        this.FindControl(CONTROLLeaseList).ControlAvailable -= LeaseList_ControlAvailable;
        DataGrid grid = (DataGrid)e.Control;
        this._LeaseGrid = grid;
        //Bind row background color to red when anything is suspended (arrears fee, late fee, invoicing).
        Binding bValue = new Binding("HasAnySuspension")
            Mode = System.Windows.Data.BindingMode.OneWay,
            Converter = new BooleanTrueToBackgroundColorConverter()
        Style rowStyle = new Style(typeof(DataGridRow));
        rowStyle.Setters.Add(new Setter(DataGridRow.BackgroundProperty, bValue));
        grid.RowStyle = rowStyle;
    private void InvoiceGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
        this.FindControl(CONTROLInvoiceGrid).ControlAvailable -= InvoiceGrid_ControlAvailable;
        DataGrid grid = (DataGrid)e.Control;
        this._InvoiceGrid = grid;
        //Show full public notes text as tooltip.
        Style cellStyleBillNotes = new Style(typeof(DataGridCell));
        Binding bPublicNotes = new Binding("PublicNotes") { Mode = System.Windows.Data.BindingMode.OneWay };
        cellStyleBillNotes.Setters.Add(new Setter(ToolTipService.ToolTipProperty, bPublicNotes));
        grid.Columns[3].CellStyle = cellStyleBillNotes;
        //Show full private notes text as tooltip.
        Style cellStyleNotes = new Style(typeof(DataGridCell));
        Binding bPrivateNotes = new Binding("InvoiceNotes") { Mode = System.Windows.Data.BindingMode.OneWay };
        cellStyleNotes.Setters.Add(new Setter(ToolTipService.ToolTipProperty, bPrivateNotes));
        grid.Columns[4].CellStyle = cellStyleNotes;
    void leaseDateSpecialTermination_ControlAvailable(object sender, ControlAvailableEventArgs e)
        this.FindControl(CONTROLLeaseDateSpecialTermination).ControlAvailable -= leaseDateSpecialTermination_ControlAvailable;
        DatePicker picker = (DatePicker)e.Control;
        picker.SelectedDateChanged += SpecialTermination_SelectedDateChanged;
    void TaxAutoComplete_ControlAvailable(object sender, ControlAvailableEventArgs e)
        this.FindControl(CONTROLTaxYear).ControlAvailable -= TaxAutoComplete_ControlAvailable;
        var taxCombo = e.Control as AutoCompleteComboBox;
        if (taxCombo != null)
            this.TaxCombo = taxCombo;
    void LeaseBalanceDue_ControlAvailable(object sender, ControlAvailableEventArgs e)
        this.FindControl(CONTROLLeaseBalanceDue).ControlAvailable -= LeaseBalanceDue_ControlAvailable;
        //string controlType = e.Control.ToString();  //System.Windows.Controls.TextBlock
        //Use red text if there is a balance due.
        TextBlock tb = (TextBlock)e.Control;
        Binding b = new Binding("Screen.Leases_ForBuildingAndOrParty.SelectedItem.BalanceDue")
            Mode = System.Windows.Data.BindingMode.OneWay,
            Converter = new BalanceToForegroundColorConverter()
    void LeaseEndDateMismatch_ControlAvailable(object sender, ControlAvailableEventArgs e)
        this.FindControl(CONTROLEndDateMismatch).ControlAvailable -= LeaseEndDateMismatch_ControlAvailable;
        TextBlock tb = (TextBlock)e.Control;
        //Hide the control unless we have text
        Binding b = new Binding("Screen.Leases_ForBuildingAndOrParty.SelectedItem.LeaseChargesEndMismatch")
            Mode = System.Windows.Data.BindingMode.OneWay,
            Converter = new TextToVisibilityConverter()
        tb.SetBinding(TextBlock.VisibilityProperty, b);
    void ChargeItemGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
        this.FindControl(CONTROLChargeItemGrid).ControlAvailable -= ChargeItemGrid_ControlAvailable;
        DataGrid grid = (DataGrid)e.Control;
        //Bind row background color to red when there is a balance due.
            //Setting style in grid.LoadingRow doesn't work any differently than setting grid.RowStyle.
            //This works, but not the first time the data is displayed. 
            // Refreshing the grid or returning to an already-visited lease displays the background.
            Binding bValue = new Binding("BalanceDue")
                Mode = System.Windows.Data.BindingMode.OneWay,
                Converter = new BalanceToBackgroundColorConverter()
            Style rowStyle = new Style(typeof(DataGridRow));
            rowStyle.Setters.Add(new Setter(DataGridRow.BackgroundProperty, bValue));
            grid.RowStyle = rowStyle;
            //This works correctly on the INITIAL display, without needing refresh or return to already-visited lease.
            Style cellStyle = new Style(typeof(DataGridCell));
            cellStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, bValue));
            grid.Columns[5].CellStyle = cellStyle;
        //Show full billing notes text as tooltip.
        Style cellStyleBillNotes = new Style(typeof(DataGridCell));
        Binding bBillNotes = new Binding("notesPrintedOnBill") { Mode = System.Windows.Data.BindingMode.OneWay };
        cellStyleBillNotes.Setters.Add(new Setter(ToolTipService.ToolTipProperty, bBillNotes));
        grid.Columns[6].CellStyle = cellStyleBillNotes;
        //Show full private notes text as tooltip.
        Style cellStyleNotes = new Style(typeof(DataGridCell));
        Binding bNotes = new Binding("notesPrivate") { Mode = System.Windows.Data.BindingMode.OneWay };
        cellStyleNotes.Setters.Add(new Setter(ToolTipService.ToolTipProperty, bNotes));
        grid.Columns[7].CellStyle = cellStyleNotes;


    Monday, October 31, 2016 10:32 PM
  • I would love to have a look at that code. I cannot see the event properties in Lightswitch the way I can say in windows forms. I just assumed you guys were hiding access to them because this was a low-code tool that originally was purchased outside of Visual Studio.

    That said I'm ready to move on to a new UI framework for data entry and since my College is teaching ASP.Net That sound like the winner.

    It is also possible that LS just tried to reinvent the wheel and had to re-write a lot of Silverlight controls etc... and the team just never had the funding or time to do that || integrate all of the event controls. Either way I'd like to have a finished UI for Hack-A-Thon.

    Monday, October 31, 2016 10:53 PM
  • Yeah It would be nice to be able to write controls so others could choose their colors in the future but for this particular set of controls I actually want the end to end solution to remain consistent so I'm choosing the colors.
    Monday, October 31, 2016 10:55 PM
  • That is really cool I have projects due the next two days for college so your code I'll break into later. After a scan It seems like I finds and adds to the existing controls logic. I can see how massive that is logic is. Unless though there are too many trees to find the end of to add code. It May be easier to take existing form control code and add conditional logic to the TextBox...based on the value as a custom control. I could use a repetitive Excel sheet string concatenates to build the code if that is needed for multiple event scenarios etc. It is also to bad that LS never has a events viewer(lightening bolt near properties). For instance in windows forms model used to directly access the events via a window. I do get we can hard code or find these events now given what I can now see;

    GridView Events

    GridView Source

    The Properties Windows I Dream Of for LS
    The Properties Windows I Dream Of for LS
    • Edited by reigh7 Tuesday, November 1, 2016 7:39 PM
    Tuesday, November 1, 2016 7:24 PM
  • That is for System.Web does this control get used in the same way with any of these namespaces;

    using System;
    using System.Linq;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Collections.Generic;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Framework.Client;
    using Microsoft.LightSwitch.Presentation;
    using Microsoft.LightSwitch.Presentation.Extensions;
    Tuesday, November 1, 2016 7:33 PM