Alex Rivera | Logout

How to bind a List<Int> to a gridview?

Asked 2009-02-05T11:10:25.903
9

This might be quite a strange question since usually people bind only complex types to a gridview. But I need to bind a List of Int (the same is for strings). Usually, as the property to bind one uses the name of the property of the object, but when using a Int or a String, the value is exactly the object itself, not a property.

What is the "name" to get the value of the object? I tried "Value", "" (empty), "this", "item" but no luck.

I'm referring to a gridview in a web form.

Update

There's a related Stack Overflow question, How to bind a List to a GridView.

Edit
Report

1 Answer

3

I expect you might have to put the data into a wrapper class - for example:

public class Wrapper<T> {
    public T Value {get;set;}
    public Wrapper() {}
    public Wrapper(T value) {Value = value;}
}

Then bind to a List<Wrapper<T>> instead (as Value) - for example using something like (C# 3.0):

var wrapped = ints.ConvertAll(
            i => new Wrapper<int>(i));

or C# 2.0:

List<Wrapper<int>> wrapped = ints.ConvertAll<Wrapper<int>>(
    delegate(int i) { return new Wrapper<int>(i); } );
answered 2009-02-05T11:15:53.113

Your Answer