0

I have an Employee class:

class Employee
{
        public string Name { get; set; }
        public string SurName { get; set; }
        public int Rest{ get; set; }
        public int  Worked { get; set; }
        public List<int> PerShift= new List<int>(); 
}

In my database, I save how many times an employee worked in 1st shift how many in 2nd etc. Then in my datagrid, I want to see how many times each employee worked in total, how many days he rested and how many days he worked in each shift. The thing is that shift number is set by user, so I have to programmatically add those columns

<DataGrid Name="EmployeesStatsDatagrid"
          Grid.Row="1"
          IsReadOnly="True"
          CanUserAddRows="False" CanUserReorderColumns="False"
          Grid.ColumnSpan="4"
          Grid.Column="0"
          IsSynchronizedWithCurrentItem="True"
          CanUserDeleteRows="False"
          CanUserResizeRows="False"
          AutoGenerateColumns="False"
          VerticalAlignment="Stretch">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
        <DataGridTextColumn Header="Surname" Binding="{Binding SureName}" />
        <DataGridTextColumn Header="Rest" Binding="{Binding Rest}" />
        <DataGridTextColumn Header="Δούλεψε" Binding="{Binding Worked}" />
    </DataGrid.Columns>
</DataGrid>

Then I add the extra columns

for (int i = 0; i < Collections.ShiftsCollection.Count; i++)
{
    DataGridTextColumn textColumn = new DataGridTextColumn();
    textColumn.Header = Collections.ShiftsCollection[i].ShiftName.ToUpper();
    //textColumn.Binding = new Binding(Collections.ShiftsCollection[i].ShiftName);
    EmployeesStatsDatagrid.Columns.Add(textColumn);
}

The columns are properly set, I create a list of Employees and binding works for name, surname, worked and rest, but how can I bind the extra columns to the List<int> PerShift? Any ideas? Or should I implement it differently?

enter image description here

For example in this case I have two shifts, morning and afternoon, first employees PerShift list is {4,0} and second is {0,1} is there a way to have those values in the column binded? I hardcoded them in this case but if I sort columns values are lost.

5
  • Please try to describe the usage and a possible content of the PerShift list. Also try to explain how you want to visualize these list inside your data grid. Commented Mar 17, 2018 at 17:17
  • PerShift.count is equal to the number of shifts and hold the times employee worked in each shift! More details added Commented Mar 17, 2018 at 17:36
  • Is there a reason why you don't add two fields Afternoon and Morning instead of using a list? Commented Mar 17, 2018 at 18:40
  • It's because the shifts are added by user and i don't know how many there will be... Commented Mar 17, 2018 at 19:34
  • But typically only the three shifts morning, afternoon, night should be avaibable? Am I right? Commented Mar 17, 2018 at 20:15

1 Answer 1

1

You can use ExpandoObject. Add your extra columns the same way you did, then create the DataGrid.ItemsSource object list as follows:

        var all_items = new List<object>();

        for (int i = 0; i <= MyData.Count-1; i++)
        {
            dynamic item = new System.Dynamic.ExpandoObject();
            item.Name = MyData[i].Name;
            item.Surname = MyData[i].Surname;
            item.Rest = MyData[i].Rest;
            item.Worked = MyData[i].Worked;
            
            for (int k = 0; k <= Collections.ShiftsCollection.Count - 1; k++)
            {
                item = ExtendExpando(item, Collections.ShiftsCollection[k].ShiftName, Collections.ShiftsCollection[k].Value);
                // or whatever corresponding value instead of Collections.ShiftsCollection[k].Value
            }

            all_items.Add(item);
        }

        EmployeesStatsDatagrid.ItemsSource = all_items;

Where ExtendExpando function can be implemented as follows:

public static object ExtendExpando(object OldExpandoObject, string NewProperty, object NewValue)
    {
        var ex = (IDictionary<string, object>)OldExpandoObject;
        ex.Add(new KeyValuePair<string, object>(NewProperty, NewValue));
        dynamic output = ex;
        return output;
    }

Note: I successfully tested this code using Visual Basic not C#, you may need some minor modifications.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.