简体   繁体   中英

Binding items of generic list to different grids in c# windows form application

I created the following classes:

public interface ISFRDataSet
    {
    }

    public interface ISFRStaticDataSet : ISFRDataSet
    {
    }

    public interface ISFRTransientDataSet : ISFRDataSet
    {
    }


    public class DataSet0 : ISFRStaticDataSet
    {
        public int Id { get; set; }

        public string Text { get; set; }
    }

    public class DataSet1 : ISFRStaticDataSet
    {
    }

    public class DataSet2 : ISFRStaticDataSet
    {
    }

    public class DataSet3 : ISFRTransientDataSet 
    {
    }

Then I created a list of ISFRDataSet and added to it an instance of DataSet1, 2 and 3 to it.

Dictionary<string, List<ISFRDataSet>> dsList = new Dictionary<string, List<ISFRDataSet>>();
            dsList.Add("DataSet1", GetDataSet1Data(segmentId));
            dsList.Add("DataSet2", GetDataSet2Data(segmentId));
            dsList.Add("DataSet3", GetDataSet3Data(segmentId));
...

This list is sent to a form, and the form iterates on each ISFRDataSet object in the list; Creates a grid for each, and binds the object to the grid.

I was expecting that the public properties of DataSet1, 2 and 3 would show in as columns in each grid. But no columns are shown, I'm assuming because ISFRDataSet interface doesn't have any property. Is there a way to accomplish what I was expecting?

Thank you

You can use this special keyword dynamic to bind gridview's datasource.

Like this workable code and all grids wiil generate columns.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Dictionary<string, List<ISFRDataSet>> dsList = new Dictionary<string, List<ISFRDataSet>>();
        dsList.Add("DataSet1", GetDataSet1Data());
        dsList.Add("DataSet2", GetDataSet2Data());
        dsList.Add("DataSet3", GetDataSet3Data());

        var list1 = new BindingList<dynamic>(DataSetConverter(dsList["DataSet1"]));
        dataGridView1.DataSource = list1;

        var list2 = new BindingList<dynamic>(DataSetConverter(dsList["DataSet2"]));
        dataGridView2.DataSource = list2;

        var list3 = new BindingList<dynamic>(DataSetConverter(dsList["DataSet3"]));
        dataGridView3.DataSource = list3;
    }

    private List<dynamic> DataSetConverter<TSource>(List<TSource> source) where TSource : class, ISFRDataSet
    {
        return source.Cast<dynamic>().ToList();
    }


    private List<ISFRDataSet> GetDataSet3Data()
    {
        return new List<DataSet3>
        {
            new DataSet3
            {
                CreateTime = DateTime.Now.AddDays(1),
                Name = "TN1"
            },
            new DataSet3
            {
                CreateTime = DateTime.Now.AddDays(3),
                Name = "TN2"
            }
        }.Cast<ISFRDataSet>().ToList();
    }

    private List<ISFRDataSet> GetDataSet2Data()
    {
        return new List<DataSet2>
        {
            new DataSet2
            {
                ProductId = 1,
                Price = 5.5M
            },
            new DataSet2
            {
                ProductId = 2,
                Price = 6.6M
            }
        }.Cast<ISFRDataSet>().ToList();
    }

    private List<ISFRDataSet> GetDataSet1Data()
    {
        return new List<DataSet1>
        {
            new DataSet1
            {
                TestId = 1,
                TestText = "t1"
            },
            new DataSet1
            {
                TestId = 2,
                TestText = "t2"
            }
        }.Cast<ISFRDataSet>().ToList();
    }
}


public interface ISFRDataSet
{
}

public interface ISFRStaticDataSet : ISFRDataSet
{
}

public interface ISFRTransientDataSet : ISFRDataSet
{
}


public class DataSet0 : ISFRStaticDataSet
{
    public int Id { get; set; }

    public string Text { get; set; }
}

public class DataSet1 : ISFRStaticDataSet
{
    public int TestId { get; set; }
    public string TestText { get; set; }
}

public class DataSet2 : ISFRStaticDataSet
{
    public int ProductId { get; set; }
    public decimal Price { get; set; }
}

public class DataSet3 : ISFRTransientDataSet
{
    public string Name { get; set; }
    public DateTime CreateTime { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM