【问题标题】:How to bind DataGridView with List<T> or BindingList<T>如何将 DataGridView 与 List<T> 或 BindingList<T> 绑定
【发布时间】:2009-05-25 18:28:01
【问题描述】:

我已经做过一千次了,它可以工作,但现在....不行:(

我在这里做错了什么,因为网格中没有显示任何内容吗?

namespace theGridIsNotWorking
{
using System;
using System.Collections.Generic;
using System.Windows.Forms;

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

    private void Form1_Load(object sender, EventArgs e)
    {
        var items = new List<Item>();

        items.Add(new Item(){ TheName = "first"});
        items.Add(new Item(){ TheName = "Second"});
        items.Add(new Item(){ TheName = "Third"});

        dataGridView1.DataSource = new List<Item>(items);
    }

    public class Item
    {
        public string TheName;
    }
}
}

没有什么壮观的......但真的很伤心。

【问题讨论】:

  • 你设置了每个DataColumn的PropertyName属性了吗?

标签: winforms data-binding datagridview


【解决方案1】:

我认为问题在于 TheName 是一个成员变量,但您需要一个属性。 对 Item 类尝试以下操作:

public class Item { public string TheName; public string TheNameProperty { get { return TheName; } } public Item(string name) { TheName = name; } }

【讨论】:

    【解决方案2】:

    试试BindingListView。将 List 绑定到 DGV 的最简单方法。

    【讨论】:

      【解决方案3】:
      BindingList<Notification>(notifications);
      

      不应该吗

      BindingList<Notification>(activeNotifications);
      

      ?

      【讨论】:

      • 抱歉错了...只是语法上...这是演示目的...必须使用activeNotifications,不起作用:(
      • 我认为这很容易......我尝试了代码,它工作正常。首先,您确定此代码已运行吗?二、你在DataGridView上设置了什么?
      • 在我的机器上,网格仍然是空的......我正在使用 vs2008,.net 3.5 ... :(
      猜你喜欢
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2012-10-25
      • 2011-09-09
      相关资源
      最近更新 更多