c# - 无法使用泛型列表填充数据 GridView

标签 c# list generics dataset

我不明白下面的简单代码有什么问题。它所做的只是获取一个列表并将其显示在数据 GridView 中。这是我得到的错误:

Error 1 Inconsistent accessibility: return type 'System.Collections.Generic.List' is less accessible than method 'WindowsFormsApplication10.Form1.FillGridView()'

此外,有没有办法将您的列表、数组列表等转换为数据集类型?

public partial class Form1 : Form
    {
    public Form1 ()
    {
        InitializeComponent ();
    }

    public List<Student> FillGridView ()
    {
        List<Student> l = new List<Student> ();
        l.Add (new Student { Fname="bloke", Lname="lll", Contact=293489485});
        l.Add (new Student { Fname = "dog", Lname = "assdf", Contact = 35345 });
        l.Add (new Student { Fname = "mary", Lname = "sdff", Contact = 6456 });
        l.Add (new Student { Fname = "john", Lname = "sdfsdf", Contact = 45656 });
        return l;


    }


    private void Form1_Load ( object sender, EventArgs e )
    {
        dataGridView1.DataSource = this.FillGridView ();
    }

    private void button1_Click ( object sender, EventArgs e )
    {

    }
}

最佳答案

你的方法需要公开吗?看起来您的学生类(class)不是公开的,因此将其列表返回给任何公开调用者是不一致的。看起来,您的方法可以是私有(private)的。

private List<Student> FillGridView()

关于c# - 无法使用泛型列表填充数据 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15542987/

相关文章:

c# - 从多个目录获取文件

c# - 在 Linux 上运行 .NET Core——什么都不写

vb.net - 找到最小值。 vb.net 中列表中的值

c# - 具有多个类的泛型

java - 取消选中返回类类型的转换

c# - System.IO.Stream 一次性使用阅读器

c# - 从已发布的单个 Exe 本地运行的 PowerShell 失败(没有 IncludeAllContentForSelfExtract)

Python - 尝试存储列表数组时陷入循环

vb.net - list<Of T> 上的多种排序

java - Generics-Type 参数如何工作?