使用对象列表填充数据网格视图

18

我有一个包含多个交易对象的列表。我想在加载窗体时,在 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]);
    }
}

这里是我得到的输出结果。 注册输出结果

3个回答

11

这个问题有两种高级方法。

1) 直接将手动创建的行添加到 DataGridView 中。在这种情况下,您必须手动更新/删除它们以响应变化。如果您只是初始化后不打算更改显示内容,则这种方法“可以”。但如果您确实需要更改内容,则这种方法就行不通了。

要直接添加它,您需要创建一个 DataGridViewRow,并将其填充为单个值,然后将 DataGridViewRow 添加到 DataGridView.Rows 中。

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

CodeProject有一篇很好的文章可以帮助您入门,但快速搜索谷歌也会得到许多其他文章。

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


我曾考虑过数据绑定,因为它似乎是最不笨拙的解决方案,可以考虑到数据源的任何更改。编辑:我按下回车键太快了。我并不完全确定数据绑定,因为我以前没有真正使用过它。我会查看你提供的链接,谢谢。 - morris295
一开始可能会有些复杂,如果你不熟悉它的话,但是随着时间和实践的积累,它会变得更容易(尽管仍然会带给你许多令人沮丧的时刻)。你上面提到的情况似乎应该是一个简单的例子,可以用来入门,祝你好运。如果你遇到更具体的问题,请随时回来咨询。 - Gjeltema
最终我使用了数据绑定,感谢您的指导,非常有帮助。你在这方面为我解决了很多麻烦。 - morris295

6

作为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
}

9
SortableBindingList<>()是什么? - Danon

5

在顶部添加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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接