views:

40

answers:

1

I'm trying to display an object into a DataGridView using Reflection

so far everything works smootly but the problem is that some of the object's properties are Lists. How can I adapt my DataGridView to show a list?

public void SetDataSource(PluginBase plugin)
{
    dgvProperties.Rows.Clear();
    List<DataGridViewRow> rows = new List<DataGridViewRow>();

    foreach (PropertyInfo info in typeof(PluginBase).GetProperties(BindingFlags.Public|BindingFlags.Instance))
    {
        object value = plugin.GetType().GetProperty(info.Name).GetValue(plugin, null);

        object[] o = new object[2];

        o[0] = info.Name;
        o[1] = value;

        DataGridViewRow dgvr = new DataGridViewRow();
        dgvr.CreateCells(dgvProperties, o);
        rows.Add(dgvr);
    }

    dgvProperties.Rows.AddRange(rows.ToArray());

}
+1  A: 

I found a pretty good tutorial that might help you: http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-collection

Update

I don't think you can automatically make a DataGridView cell display a list, but you might be able do it once your reflection detects it's list then you can manually do the following: http://msdn.microsoft.com/en-us/library/aa480727.aspx

Lirik
I know how to do this, the problem is when one of the field is a List, it wont display properly
Eric
Are you trying to display a list inside a grid cell?
Lirik
yeah, something like that. I want the user to be able to edit the list if possible
Eric
@Eric, I updated my answer... you'd have to check if the Property is a list (or if it implements an enumerable type), then you'd have to create a drop-down list inside the given cell. The drop-down list will contain all of the elements that are in the collection.
Lirik