简体   繁体   English

如何在C#中的DataGridView中显示对象的内容?

[英]How do I present the contents of my object in DataGridView in C#?

Here is my code: 这是我的代码:

void Main() {
    List<Restaurant> restaurants = new List<Restaurant>();
}

class Restaurant {
    public int Id;
    public List<Complaint> Complaints = new List<Complaints>();
}

class Complaint {
    public string Name;
    public string Address;
    public int Age;
    public DateTime ComplaintDate;
}

I'd like to present the data contained in RestaurantData, an object of class Restaurant, in the DataGridView control. 我想在DataGridView控件中显示包含在RestaurantData类中的RestaurantData对象中的数据。 First I'd like to show the Restaurant's ID, followed by the number of Complaints against the restaurant so that it looks something like this (where the ID is in column A, and the Name is in Column B, and so forth): 首先,我想显示餐厅的ID,然后显示对餐厅的投诉数量,以使其看起来像这样(其中ID在A列中,名称在B列中,依此类推):

  • ID Name Address Age Date ID名称地址年龄日期
  • Name Address Age Date 姓名地址年龄日期
  • Name Address Age Date 姓名地址年龄日期

My past usage of DataGridView has been limited to dataGridView1.DataSource = (some array). 我过去对DataGridView的用法仅限于dataGridView1.DataSource =(某些数组)。 However, the information I want to present this time is clearly not an array so I'm stuck. 但是,这次我想提供的信息显然不是数组,因此我很困惑。

If you want to cull the columns to your own list, something like this works: 如果要将列剔除到自己的列表中,则可以使用以下方法:

private void AddColumns()
{
    dataGridView1.AutoGenerateColumns = false;
    DataGridViewTextBoxColumn ageColumn =
        new DataGridViewTextBoxColumn();
    ageColumn.Name = "Age";
    ageColumn.DataPropertyName = "Age";
    ageColumn.ReadOnly = true;

    DataGridViewTextBoxColumn nameColumn =
        new DataGridViewTextBoxColumn();
    nameColumn.Name = "Name";
    nameColumn.DataPropertyName = "Name";
    nameColumn.ReadOnly = true;

    DataGridViewTextBoxColumn addressColumn =
        new DataGridViewTextBoxColumn();
    addressColumn.Name = "Address";
    addressColumn.DataPropertyName = "Address";
    addressColumn.ReadOnly = true;

    DataGridViewTextBoxColumn dateColumn =
        new DataGridViewTextBoxColumn();
    addressColumn.Name = "Date";
    addressColumn.DataPropertyName = "Date";
    addressColumn.ReadOnly = true;

    dataGridView1.Columns.Add(ageColumn);
    dataGridView1.Columns.Add(nameColumn);
    dataGridView1.Columns.Add(addressColumn);
    dataGridView1.Columns.Add(dateColumn);

    var ds = (from r in restaurants
              from c in r.Complaints
              select new {Id = r.Id, Address =c.Address, Age = c.Age, Name = c.Name, Date = c.ComplaintDate}
             ).ToList();
    dataGridView1.DataSource = ds;
}

If you just want to autogenerate the columns try this: 如果您只想自动生成列,请尝试以下操作:

private void AddColumns()
{
    dataGridView1.AutoGenerateColumns = true;

    var ds = (from r in restaurants
              from c in r.Complaints
              select new {Id = r.Id, Address =c.Address, Age = c.Age, Name = c.Name, Date = c.ComplaintDate}
             ).ToList();
    dataGridView1.DataSource = ds;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM