8

I have an array, arrStudents, that contains my students' age, GPA, and name like so:

arrStudents[0].Age = "8"
arrStudents[0].GPA = "3.5"
arrStudents[0].Name = "Bob"

I tried to bind arrStudents to a DataGridView like so:

dataGridView1.DataSource = arrStudents;

But the contents of the array do NOT show up in the control. Am I missing something?

1
  • Along with what others have written, I'd be inclined to use a BindingList<T> so that changes to the underlying data would be visible in the DataGridView. Commented Sep 7, 2012 at 19:04

2 Answers 2

10

As with Adolfo, I've verified that this works. There is nothing wrong in the code shown, so the problem must be in the code you aren't showing.

My guess: Age etc are not public properties; either they are internal or they are fields, i.e. public int Age; instead of public int Age {get;set;}.

Here's your code working for both a well-typed array and an array of anonymous types:

using System;
using System.Linq;
using System.Windows.Forms;
public class Student
{
    public int Age { get; set; }
    public double GPA { get; set; }
    public string Name { get; set; }
}

internal class Program
{
    [STAThread]
    public static void Main() {
        Application.EnableVisualStyles();
        using(var grid = new DataGridView { Dock = DockStyle.Fill})
        using(var form = new Form { Controls = {grid}}) {
            // typed
            var arrStudents = new[] {
                new Student{ Age = 1, GPA = 2, Name = "abc"},
                new Student{ Age = 3, GPA = 4, Name = "def"},
                new Student{ Age = 5, GPA = 6, Name = "ghi"},
            };
            form.Text = "Typed Array";
            grid.DataSource = arrStudents;
            form.ShowDialog();

            // anon-type
            var anonTypeArr = arrStudents.Select(
                x => new {x.Age, x.GPA, x.Name}).ToArray();
            grid.DataSource = anonTypeArr;
            form.Text = "Anonymous Type Array";
            form.ShowDialog();
        }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hello Marc. I'm confused. What am I doing wrong, and why isn't the contents of my array showing up in the DataGridView?
@phan what is the exact type of arrStudents, and what does Student look like ?
Well, the exact type can be found in the answer I chose for this question: stackoverflow.com/questions/12321842/…. "arrSummary" in that solution s what my arrStudents array is like. If I can bind the arrSummary in that question to a DataGridView I would be happy as well.
@phan the code in your accepted answer is an anonymous type; which in c# does not have writeable properties, so it is inconsistent with the code shown in your question. I've also played with this using anonymous types, and it still works.
Wow, the lightbulb went off in my head. Thank you Marc!!! There was no intention to "lie". I honestly described the problem as I saw it, but my understanding of it is, as you can see, wrong.
|
10

This works for me:

public class Student
{
    public int Age { get; set; }
    public double GPA { get; set; }
    public string Name { get; set; }
}

public Form1()
{
        InitializeComponent();

        Student[] arrStudents = new Student[1];
        arrStudents[0] = new Student();
        arrStudents[0].Age = 8;
        arrStudents[0].GPA = 3.5;
        arrStudents[0].Name = "Bob";

        dataGridView1.DataSource = arrStudents;
}

Or less redundant:

arrStudents[0] = new Student {Age = 8, GPA = 3.5, Name = "Bob"};

I'd also use a List<Student> instead of an array since it will have to grow most likely.

Is That what you're doing too?

enter image description here

3 Comments

I've tried this but it doesn't work. I used an ArrayList though and filled with elements of a class I derived. Does this matter?
Figured it out. I was using public members for the class instead of properties. Weird though!
Will this automatically add elements to the list when the user includes new lines?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.