views:

30

answers:

2
List A = new List();
A.Add("Apple");
A.Add("Banana");
A.Add("Pineapple");

dataGridView.DataSource = a;

Result: is the length of each item in the list rather than Item itself. 5 6 9 How can I make datagridView to display string instead of length.

+2  A: 

create a class, Fruit

class Fruit
{
    public string Name { get; set; }
}

and create a generic list of fruits:

List<Fruit> fruits = new List<Fruit>();
fruits.Add(new Fruit() { Name = "Apple" });

dataGridView1.DataSource = fruits;
Sebastian Brózda
Thanks a lot. This is what I was looking for.
Ani
+2  A: 

you can also do some funky LINQ

List<string> A = new List<string>(); A.Add("Apple"); A.Add("Banana"); A.Add("Pineapple");
dataGridView.DataSource = (from a in A select new {Value = a}).ToList();

edit

To explain a bit further, the issue is that the datagridview is binding to the default property of the object (so a string is length) there is no real property in a string (like value for instance) for you to set DataMember too so you have to create a class, or in my case give it an anonymous class with just one property (or many properties and set DataMember)

Pharabus
this is second time. I sure I will never forget this.thank a lot for brief explanation.
Ani