【问题标题】:populating datagridview with list of objects用对象列表填充 datagridview
【发布时间】:2013-04-21 22:16:35
【问题描述】:

我有一个包含一系列交易对象的列表。我要做的是在加载表单时在 Datagridview 控件中显示这些事务对象,基本上 Datagridview 应该代表事务寄存器的某些内容,以显示列表中每个事务对象的数据。

我必须承认在使用 Datagridviews 时缺乏经验,而且我在理解我需要在这里做什么时遇到了一些困难。

我的问题是,如何获取列表中每个对象的详细信息以显示在 Datagridview 中?

这是我的代码。

首先是事务类:

public class Transaction
{
    // Class properties
    private decimal amount;
    private string type;
    private decimal balance;
    private string date;
    private string transNum;
    private string description;

    // Constructor to create transaction object with values set.
    public Transaction(decimal amount, string type, decimal currBal, string date, string num, string descrip)
    {
        this.amount = amount;
        this.type = type;
        this.balance = currBal;
        this.date = date;
        this.transNum = num;
        this.description = descrip;
    }

    // Get and Set accessors to allow manipulation of values.
    public decimal Amount
    {
        get
        {
            return amount;
        }
        set
        {
            amount = value;
        }
    }
    public string Type
    {
        get
        {
            return type;
        }
        set
        {
            type = value;
        }
    }
    public decimal Balance
    {
        get
        {
            return balance;
        }
        set
        {
            balance = value;
        }
    }
    public string Date
    {
        get
        {
            return date;
        }
        set
        {
            date = value;
        }
    }
    public string TransNum
    {
        get
        {
            return transNum;
        }
        set
        {
            transNum = value;
        }
    }
    public string Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }

    public decimal addCredit(decimal balance, decimal credit)
    {
        decimal newBalance;
        newBalance = balance + credit;
        return newBalance;
    }

    public decimal subtractDebit(decimal balance, decimal debit)
    {
        decimal newBalance;
        newBalance = balance - debit;
        return newBalance;
    }
    }
}

现在是“注册”表单的代码:

    public partial class Register : Form
{
    List<Transaction> tranList = new List<Transaction>();

    public Register(List<Transaction> List)
    {
        InitializeComponent();
        this.tranList = List;
    }

    private void Register_Load(object sender, System.EventArgs e)
    {
        //regView represents the Datagridview that I'm trying to work with
        regView.AutoSize = true;
        regView.DataSource = tranList;
        regView.Rows.Add(tranList[0]);
    }
}

这是我得到的输出。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    实际上有两种高级方法。

    1) 将手动创建的行直接添加到DataGridView。在这种情况下,您必须随着事情的变化手动更新/删除它们。如果您不打算在初始化后更改/更改显示的内容,这种方法是“可以的”。这样做就站不住脚了。

    要直接添加它,您需要创建一个DataGridViewRow,并用各个值填充它,然后将DataGridViewRow 添加到DataGridView.Rows

    2) 数据绑定 DGV。有很多关于数据绑定到DataGridView 的文章。在某些情况下,将数据添加到DataTable,然后从中提取DataView,并将DataGridView 绑定到DataView 会更容易。其他人发现直接绑定到集合更容易。

    CodeProject 有一篇不错的文章可以帮助您开始这条道路,但快速的 Google 搜索会产生许多其他文章。

    http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial

    【讨论】:

    • 我曾考虑过数据绑定,因为它似乎是最简单的解决方案,因为它考虑了对数据源的任何更改。编辑:我输入太快了。我想说我对数据绑定并不完全确定,因为我以前没有真正做过。我会看看你提供的链接,谢谢。
    • 一开始你不熟悉它会有点复杂,但随着时间和练习它会变得更容易(尽管它仍然会给你带来许多令人沮丧的时刻)。您上面的情况似乎应该是一个简单的入门示例,祝您好运。如果您遇到更具体的问题,请随时回来。
    • 我最终使用了数据绑定,感谢您的指导,它很有帮助。你为我省去了很多悲伤。
    【解决方案2】:

    用作 DGV:

    DataGridView groupListDataGridView;
    

    列:

    DataGridViewTextBoxColumn groupListNameColumn;
    

    列设置应该是这样的:

    groupListNameColumn.DataPropertyName = "name";
    

    使用此属性,否则将添加所有列。

    groupListDataGridView.AutoGenerateColumns = false;
    

    这样填充:

    private void populateGroupList() {
        groupListDataGridView.DataSource = null;
        formattedGroupList = new SortableBindingList<DataGridGroupObject>();
        foreach (GroupObject go in StartUp.GroupList) {
            DataGridGroupObject dggo = new DataGridGroupObject();
            dggo.id = go.Id;
            dggo.name = go.Name;
            formattedGroupList.Add(dggo);
        }
        groupListDataGridView.DataSource = formattedGroupList;
        groupListDataGridView.Invalidate();
    }
    

    和型号:

    public class DataGridGroupObject
    {
        public int id { get; set; }      //this will be match id column
        public string name { get; set; } // this will be match name column
    }
    

    【讨论】:

    • 什么是 SortableBindingList() ?
    【解决方案3】:

    只需在顶部添加using System.Linq;。然后你可以这样做:

    //This will create a custom datasource for the DataGridView.
    var transactionsDataSource = tranList.Select(x => new
    {
            Amount = x.amount,
            Type = x.type,
            Balance = x.balance,
            Date = x.date,
            TransNum = x.transNum
            Description = x.description
    }).ToList();
    
    //This will assign the datasource. All the columns you listed will show up, and every row
    //of data in the list will populate into the DataGridView.
    regView.DataSource = transactionsDataSource;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      相关资源
      最近更新 更多