Amit Bisht
Amit Bisht

Reputation: 5136

is there a way to make a list of two differnt type

I am trying to make a list which is used to store data source of a data grid view but the type of data source of data grid view is either "string" or "decimal"

I made this code

List<string> list1 = (List<string>)DataGridView.Datasource;
List<decimal> list2 = (List<decimal>)DataGridView.Datasource;

Two different type of list but I want to do this by declaring only one list please help me..

Upvotes: 0

Views: 260

Answers (5)

JeremiahDotNet
JeremiahDotNet

Reputation: 910

Since no one else mentioned this yet...

There is also a CompositeCollection that you can use to store different types of objects in one IList compatible collection.

public class MyObject()
{
    public string SomeData { get; set; }
    public int SomeOtherData { get; set; }
}

public class MyOtherObject()
{
    public Guid ID { get; set; }
    public object Foo { get; set; }
}

public class Main()
{
    private List<MyObject> objects = new List<MyObject>();
    private List<MyOtherObject> moreObjects = new List<MyOtherObject>();

    public CompositeCollection TheCollection { get; private set; }

    public Main()
    {
        //mock adding data to the list
        objects.Add( ... );
        moreObjects.Add ( ... );

        //Build the composite collection
        TheCollection = new CompositeCollection
        {
            new CollectionContainer {Collection = objects},
            new CollectionContainer {Collection = moreObjects}                  
        };
    }
}

Upvotes: 0

Irfan
Irfan

Reputation: 2771

List<object> genericList = (List<object>)DataGridView.datasource;

would it solve the issue?

Here is sample conversion using LINQ.

List<decimal> dList = new List<decimal>();
List<object> oList = new List<object>();
oList = dList.Select(x => (object)x).ToList<object>();

Upvotes: -1

Servy
Servy

Reputation: 203814

If you need to you can cast the data source to IList (the non-generic version).

IList data = (IList)DataGridView.Datasource;

This will allow you to access the items in the list as object types.

Another option would be to simply check the type of the list and act accordingly:

if(DataGridView.Datasource is List<string>)
{
    List<string> list = (List<string>)DataGridView.Datasource;
}
else
{
    List<decimal> list = (List<decimal>)DataGridView.Datasource;
}

This would be what you would do if you needed to have the objects typed as either a string or decimal, and couldn't work with them just as objects.

Upvotes: 2

Ant P
Ant P

Reputation: 25221

If DataGridView.datasource is a List<decimal> then you will need to instantiate a new list and add each object manually:

List<object> myList = new List<object>();
foreach(var item in DataGridView.datasource)
{
    myList.Add(item);
}

But as DanielMann says, you really should define a proper class to handle your two types of object, rather than just shoehorning them into a List<object>...

Upvotes: 0

user2711965
user2711965

Reputation: 1825

use List<object> or worse ArrayList (Don't use ArrayList please).

List<object> list = (List<object>)DataGridView.datasource;

You can also look into List<dynamic>

List<dynamic> list = new List<dynamic>();
list.Add(1);
list.Add("ABCD");
list.Add(1f);

foreach (var item in list)
{
    Console.WriteLine(item.GetType());
}

and the output would be:

System.Int32
System.String
System.Single

(Not really sure if casting the DataSource from the GridView is possible for List<dynamic>)

Upvotes: 4

Related Questions