C# 是否可以将数组绑定到 DataGridView 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12323596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:53:32  来源:igfitidea点击:

Is it possible to bind an array to DataGridView control?

c#winformsbinding

提问by phan

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

我有一个数组 arrStudents,其中包含我学生的年龄、GPA 和姓名,如下所示:

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

I tried to bind arrStudents to a DataGridView like so:

我尝试将 arrStudents 绑定到 DataGridView,如下所示:

dataGridView1.DataSource = arrStudents;

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

但是数组的内容不会显示在控件中。我错过了什么吗?

采纳答案by Marc Gravell

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: Ageetc are not public properties; either they are internalor they are fields, i.e. public int Age;instead of public int Age {get;set;}.

我的猜测:Age等不是公共财产;它们要么是internal要么是字段,即public int Age;代替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();
        }
    }
}

回答by Adolfo Perez

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.

我也会使用 aList<Student>而不是数组,因为它很可能必须增长。

Is That what you're doing too?

这也是你在做的吗?

enter image description here

在此处输入图片说明