0

I want to display a grid where it shows invitee information and - and that's the tricky part - columns indicating which mails they have received.

For instance, a grid like this:

Firstname | Lastname | ReceivedMail1 | ReceivedMail2

If a third mail is created in the application, I the grid needs to add a column like this:

Firstname | Lastname | ReceivedMail1 | ReceivedMail2 | ReceivedMail3

My database is relational, so there's someting like person.receivedEmails with a collection of e-mails.

I'm looking for a best practice to go about this. I'm working in C# in a WinForms app, but I need this for my webapp too. I am considering a dynamic object (although I'm not sure how good that works with the grid), or maybe a database view. Any suggestions?

Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64

1 Answers1

0

OK, this is how I'm going to do it. Thanks to Adding-properties-To-ExpandoObjects and Binding-ExpandoObjects-ToDatagrid

Here's a prototype:

        dynamic obj = new ExpandoObject();
        obj.FirsName = "John";
        obj.LastName = "Doe";
        var x = obj as IDictionary<string, Object>;
        for (int i = 0; i < 3; i++)
        {
            x["ReceivedMail_" + i] = true;
        }

        var data = new List<ExpandoObject>() { obj };
        this.dataGridView1.DataSource = data.ToDataTable();

using that method from the 2nd link:

    public static class DynamicIENumerableExtension
{
    public static DataTable ToDataTable(this IEnumerable<dynamic> items)
    {
        var data = items.ToArray();
        if (data.Count() == 0) return null;

        var dt = new DataTable();
        foreach (var key in ((IDictionary<string, object>)data[0]).Keys)
        {
            dt.Columns.Add(key);
        }
        foreach (var d in data)
        {
            dt.Rows.Add(((IDictionary<string, object>)d).Values.ToArray());
        }
        return dt;
    }

Result is EXACTLY like I wanted. :)

Result

Community
  • 1
  • 1
Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64