【问题标题】:Using a list<Item> as DataSource in a Gridview在 Gridview 中使用 list<Item> 作为 DataSource
【发布时间】:2016-12-12 03:05:57
【问题描述】:

我是 DataGridView 的新手,我正在尝试使用 List 作为 DataSource。

使用我当前的代码,我的网格会显示 3 行,其中包含空数据。谁能告诉我这有什么问题。

这是我的代码:

物品

    class Item
{
    public int id;
    public string name;
    public string imagePath;
    public int type;
    public int hp;
    public int mp;
    public int str;
    public int dex;
    public int vit;
    public int agi;
    public int iInt;
    public int mnd;
    public int att;
    public int acc;
    public int def;
    public int eva;
    public int matt;
    public int macc;
    public string text;

    public Item()
    {

    }

    public Item(int Id, string Name, string ImagePath, int STR, int DEX, int VIT, int AGI, int INT, int MND,
                int ATT, int ACC, int DEF, int EVA, int MATT, int MACC, int HP, int MP, int Type, string Text)
    {
        id = Id;
        name = Name;
        imagePath = ImagePath;
        type = Type;
        str = STR;
        dex = DEX;
        vit = VIT;
        agi = AGI;
        iInt = INT;
        mnd = MND;
        att = ATT;
        acc = ACC;
        def = DEF;
        eva = EVA;
        matt = MATT;
        macc = MACC;
        text = Text;
    }

这是我的表单代码:

    datagridItems.Rows.Clear();

        List<Item> items = new List<Item>();

        Connection connection = new Connection();
        string requeteItems = "SELECT * FROM Items";

        connection.Open();
        SqlDataReader myReader = connection.Read(requeteItems);

        while (myReader.Read())
        {
            Item item = new Item();

            item.id = int.Parse(myReader["Id"].ToString());
            item.name = myReader["Name"].ToString();
            item.imagePath = myReader["Image"].ToString();
            item.hp = int.Parse(myReader["HP"].ToString());
            item.mp = int.Parse(myReader["MP"].ToString());
            item.str = int.Parse(myReader["STR"].ToString());
            item.dex = int.Parse(myReader["DEX"].ToString());
            item.vit = int.Parse(myReader["VIT"].ToString());
            item.agi = int.Parse(myReader["AGI"].ToString());
            item.iInt = int.Parse(myReader["INT"].ToString());
            item.mnd = int.Parse(myReader["MND"].ToString());
            item.att = int.Parse(myReader["ATT"].ToString());
            item.acc = int.Parse(myReader["ACC"].ToString());
            item.def = int.Parse(myReader["DEF"].ToString());
            item.eva = int.Parse(myReader["EVAS"].ToString());
            item.matt = int.Parse(myReader["MATT"].ToString());
            item.macc = int.Parse(myReader["MACC"].ToString());
            item.text = myReader["Text"].ToString();
            item.type = 0;
            items.Add(item);

        }

        connection.Close();

        bsItems.DataSource = items;
    }

连接返回数据没有问题。我想知道为什么我的行有空白数据。

【问题讨论】:

  • 通过将; 替换为{ get; set; },将Item 类字段转换为属性
  • 试试 bsItems.DataBind()

标签: c# winforms datagridview


【解决方案1】:

试试这个,

var bindingList = new BindingList<Item>(items);
var source = new BindingSource(bindingList, null);
grid.DataSource = source;

【讨论】:

    【解决方案2】:

    参考:
    How to bind list to dataGridView?
    Binding List<T> to DataGridView in WinForm

    使用BindingList 并设置DataPropertyName-列的属性。

    示例:

    var list = new List<Person>()
    {
        new Person { Name = "Joe", },
        new Person { Name = "Misha", },
    };
    var bindingList = new BindingList<Person>(list);
    var source = new BindingSource(bindingList, null);
    grid.DataSource = source;
    

    更多绑定相关信息..

    Binding Controls to Data Created at Runtime

    假设您有一个表示数据记录的对象,并且这些记录的列表应显示在数据感知控件中。要允许将此列表绑定到控件,您可以执行以下操作之一:

    1. 使用 System.ComponentModel.BindingListSystem.Collections.Generic.List 泛型类型来创建列表。与 List 类不同,BindingList 类支持更改通知。绑定到此数据源后,数据感知控件将在基础数据更改时自行更新。
    2. 创建一个封装记录列表的类并实现IListIListSourceITypedListIBindingList接口对于这堂课。下一节将介绍这些接口之间的区别。

    首先将字段转换为属性声明,然后使用 BindingList 启用控件的全部功能。

    参考资料:
    grid control - how to bind grid datasource to List ?
    Bind DataGrid to Generic List<> will not allow Add, Edit items in Grid
    Problem with data binding to DevExpress XtraGrid

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      相关资源
      最近更新 更多