用对象列表填充 datagridview [英] populating datagridview with list of objects
问题描述
我有一个包含一系列交易对象的列表.我想要做的是在加载表单时在 Datagridview 控件中显示这些事务对象,基本上 Datagridview 应该代表事务寄存器的某些内容,以显示列表中每个事务对象的数据.
I have a List that contains a series of transaction objects. What I'm trying to do is to display these transaction objects in a Datagridview control on loading a form, basically the Datagridview should represent something of a transaction register to display the data for each of the transaction objects in the list.
我必须承认我在使用 Datagridviews 方面缺乏经验,并且在理解我需要在这里做什么方面遇到了一些困难.
I must admit to a lack of experience when it comes to using Datagridviews and I'm having some difficulty with understanding what I need to do here.
我的问题是,如何获取列表中每个对象的详细信息以显示在 Datagridview 中?
My question is, how do I go about getting the details of each of the objects in the list to display in the 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;
}
}
}
现在是注册"的代码.形式:
Now the code for the "Register" form:
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]);
}
}
这是我得到的输出.
推荐答案
实际上有两种高级方法可以解决这个问题.
There's really two high level approaches to this.
1) 将手动创建的行直接添加到 DataGridView
.在这种情况下,您必须在情况发生变化时手动更新/删除它们.如果您在初始化后不打算更改/更改显示内容,则此方法可以".如果你这样做,它就会变得站不住脚.
1) Add the manually created rows directly to the DataGridView
. In this case, you have to manually update/remove them as things change. This approach is "ok" if you don't intend to alter/change the content of the display after you initialize it. It becomes untenable if you do.
要直接添加它,您需要创建一个DataGridViewRow
,并用各个值填充它,然后将DataGridViewRow
添加到DataGridView.Rows
.
To add it directly, you need to create a DataGridViewRow
, and populate it with the individual values, and then add the DataGridViewRow
to the DataGridView.Rows
.
2) 数据绑定 DGV.有很多关于数据绑定到 DataGridView
的文章.在某些情况下,将数据添加到 DataTable
,然后从中提取 DataView
,并将 DataGridView
绑定到数据视图
.其他人发现直接绑定到集合更容易.
2) Data bind the DGV. There's many articles about databinding to a DataGridView
. In some cases, it's easier to just add your data to a DataTable
, and then extract a DataView
from that, and bind the DataGridView
to the DataView
. Other people find it easier to directly bind to a collection.
CodeProject 有一篇不错的文章可以帮助您开始这条道路,但在 Google 上快速搜索会产生许多其他文章.
CodeProject has a decent article to get you started down that path, but a quick Google search will yield many other articles.
http://www.codeproject.com/Articles/24656/A-详细数据绑定教程
这篇关于用对象列表填充 datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!