locked
How to get a list of numerical values in a column. RRS feed

  • Question

  • I have a table with Ages example:

     

    How do I get each value?


    • Edited by gnetsys Wednesday, August 12, 2015 7:15 PM
    Wednesday, August 12, 2015 7:13 PM

Answers

  • Hi gnetsys,

    Please try with find the DataGrid control and then get its ItemSource to get all ages in the DataGrid as below code.

            partial void GetDataInGrid_Execute()
            {
                this.FindControl("grid").ControlAvailable += EditableCustomersGrid_ControlAvailable;
            }
     
            void EditableCustomersGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                List<int> ageList = new List<int>();
                if (e.Control is DataGrid)
                {
                    DataGrid dg = (DataGrid)e.Control;
                    foreach (Customer c in dg.ItemsSource)
                    {
                        ageList.Add(c.Age);
                    }
                }
            }

      




    Best Regards,
    Weiwei

    • Marked as answer by gnetsys Wednesday, August 19, 2015 9:08 AM
    Thursday, August 13, 2015 9:53 AM
    Moderator

All replies

  • Hi gnetsys,

    Where do you want to get these value? In the screen's InitializeDataWorkspace, we can get all objects that will load to screen. Please try with below code which foreach all objects that shown on the screen.

            partial void EditableCustomersGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
            {
                // Write your code here.
                int totalAge = 0;
                int count = 0;
     
                foreach (Customer c in Customers)
                {
                    totalAge += c.Age;
                    count++;
                }
                int avg = totalAge / count;
            }

      




    Best Regards,
    Weiwei

    Thursday, August 13, 2015 7:43 AM
    Moderator
  • The code reference produces a single summary value. I would like to produce a list of each value that is contained in each row. 

    Thanks

    Thursday, August 13, 2015 8:35 AM
  • Hi gnetsys,

    Please try with find the DataGrid control and then get its ItemSource to get all ages in the DataGrid as below code.

            partial void GetDataInGrid_Execute()
            {
                this.FindControl("grid").ControlAvailable += EditableCustomersGrid_ControlAvailable;
            }
     
            void EditableCustomersGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                List<int> ageList = new List<int>();
                if (e.Control is DataGrid)
                {
                    DataGrid dg = (DataGrid)e.Control;
                    foreach (Customer c in dg.ItemsSource)
                    {
                        ageList.Add(c.Age);
                    }
                }
            }

      




    Best Regards,
    Weiwei

    • Marked as answer by gnetsys Wednesday, August 19, 2015 9:08 AM
    Thursday, August 13, 2015 9:53 AM
    Moderator
  • How do I display this value?
    Wednesday, August 19, 2015 2:07 PM
  • Hi gnetsys,

    Where do you want to display? According to your first post, they have been show in your DataGrid and you get these values from the DataGrid.
    If you want to show these values at any other place on this screen, I think below article is very helpful. Hope it can help you.
    http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/17/It-Is-Easy-To-Display-Counts-And-Percentages-In-LightSwitch.aspx
     
    Best Regards,
    Weiwei

    Thursday, August 20, 2015 6:43 AM
    Moderator