C# 用对象列表填充 datagridview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16258468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
populating datagridview with list of objects
提问by morris295
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.
我有一个包含一系列交易对象的列表。我想要做的是在加载表单时在 Datagridview 控件中显示这些事务对象,基本上 Datagridview 应该代表事务寄存器的某些内容,以显示列表中每个事务对象的数据。
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.
我必须承认在使用 Datagridviews 时缺乏经验,而且我在理解我需要在这里做什么时遇到了一些困难。
My question is, how do I go about getting the details of each of the objects in the list to display in the Datagridview?
我的问题是,如何获取列表中每个对象的详细信息以显示在 Datagridview 中?
Here is my code.
这是我的代码。
First the transaction class:
首先是事务类:
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]);
}
}
And here's the output I get.

这是我得到的输出。

采纳答案by Gjeltema
There's really two high level approaches to this.
对此,确实有两种高级方法。
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.
1) 将手动创建的行直接添加到DataGridView. 在这种情况下,您必须在情况发生变化时手动更新/删除它们。如果您在初始化后不打算更改/更改显示内容,则此方法“可以”。如果你这样做,它就会变得站不住脚。
To add it directly, you need to create a DataGridViewRow, and populate it with the individual values, and then add the DataGridViewRowto the DataGridView.Rows.
要直接添加它,您需要创建一个DataGridViewRow,并用各个值填充它,然后将 添加DataGridViewRow到DataGridView.Rows.
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 DataViewfrom that, and bind the DataGridViewto the DataView. Other people find it easier to directly bind to a collection.
2) 数据绑定DGV。有很多关于数据绑定到DataGridView. 在某些情况下,只需将数据添加到 a 中DataTable,然后从中提取 a DataView,然后将 绑定DataGridView到DataView. 其他人发现直接绑定到集合更容易。
CodeProject has a decent article to get you started down that path, but a quick Google search will yield many other articles.
CodeProject 有一篇不错的文章可以帮助您走上这条道路,但是在 Google 上快速搜索会产生许多其他文章。
http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
回答by Onder Ezdi
use as DGV:
用作 DGV:
DataGridView groupListDataGridView;
column:
柱子:
DataGridViewTextBoxColumn groupListNameColumn;
column setup should be like this:
列设置应如下所示:
groupListNameColumn.DataPropertyName = "name";
use this property, else all columns will be added.
使用此属性,否则将添加所有列。
groupListDataGridView.AutoGenerateColumns = false;
populate like this:
像这样填充:
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();
}
and model:
和型号:
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
}

