Using a List as a Data Source for Datagridview

Using a list as a data source for DataGridView

First, I don't understand why you are adding all the keys and values count times, Index is never used.

I tried this example :

        var source = new BindingSource();
List<MyStruct> list = new List<MyStruct> { new MyStruct("fff", "b"), new MyStruct("c","d") };
source.DataSource = list;
grid.DataSource = source;

and that work pretty well, I get two columns with the correct names. MyStruct type exposes properties that the binding mechanism can use.

    class MyStruct
{
public string Name { get; set; }
public string Adres { get; set; }

public MyStruct(string name, string adress)
{
Name = name;
Adres = adress;
}
}

Try to build a type that takes one key and value, and add it one by one.
Hope this helps.

Using a listItem as DataSource in a Gridview

Try this,

var bindingList = new BindingList<Item>(items);
var source = new BindingSource(bindingList, null);
grid.DataSource = source;

Listint collecion cannot be used as datasource as DataGridView

The reason it's working the it does, is that DataGridView looks for properties to show on the object. String has one property, Length, so it's shown on the grid. Integer doesn't have any properties.

To make it work, you could create a new class that has a property of type int. You can then give a list of those custom objects to the DataGridView and control what is shown.

Edit: This answer has an example on how to solve your problem.

Gridview using a generic list as DataSource and Auto-generating columns

Try adjusting your student class and change your fields into properties like this:

public class student
{
public string name { get; set; }
public string address { get; set; }
}

Binding ListT to DataGridView in WinForm

List does not implement IBindingList so the grid does not know about your new items.

Bind your DataGridView to a BindingList<T> instead.

var list = new BindingList<Person>(persons);
myGrid.DataSource = list;

But I would even go further and bind your grid to a BindingSource

var list = new List<Person>()
{
new Person { Name = "Joe", },
new Person { Name = "Misha", },
};
var bindingList = new BindingList<Person>(list);
var source = new BindingSource(bindingList, null);
grid.DataSource = source;

How can I use a ListDynamic as with DataGridView.DataSource?

If I remember correctly, Dapper's dynamic query returns a collection of ExpandoObjects that lets you dynamically access properties such as person.Name, but the underlying objects doesn't actually have a Name property. It uses run-time binding to extract the data from an internal key/value dictionary. Since the default data binding for DataGridView uses reflection to get the properties of the objects, it does not find the columns returned from the query.

So you have a few options:

  • Hydrate the result as a concrete type instead of dynamic
  • Specify the columns you want to display in your DataGridView rather than using the default binding.
  • Convert the dynamic result to a DataTable using something similar to this answer.

How to fill the DataGridview Datasource with the List?

you can use BindingList
for example :

protected void GenerateButton_Click(object sender, EventArgs e)
{
BindingList<ListType> SerialNumberList = new BindingList<ListType>();
int x = 5;
SerialNumberList.Add(new ListType(x.ToString()));

grid.DataSource = SerialNumberList;
grid.DataBind();
}
}
public class ListType
{
public ListType()
{ }
private string Itemname;

public ListType(string _ListItem)
{
ListItem = _ListItem;
}
public string ListItem
{
get { return Itemname; }
set { Itemname = value; }
}

}


Related Topics



Leave a reply



Submit