使用对象列表填充datagridview [英] populating datagridview with list of objects

查看:181
本文介绍了使用对象列表填充datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在使用Datagridviews时,我必须承认缺乏经验,而且我在理解我需要做的工作方面遇到困难。



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



这是我的代码



首先是事务类:

  public class Transaction 
{
//类属性
私有十进制数;
私有字符串类型;
私有十进制余额;
私人字符串日期;
private string transNum;
私人字符串描述;

//构造器创建值设置的事务对象。
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;
}

//获取和设置访问器以允许操纵值。
public decimal金额
{
get
{
return amount;
}
设置
{
amount = value;
}
}
public string Type
{
get
{
return type;
}
set
{
type = value;
}
}
公共小数余额
{
获取
{
返回余额;
}
set
{
balance = value;
}
}
public string Date
{
get
{
return date;
}
set
{
date = value;
}
}
public string TransNum
{
get
{
return transNum;
}
set
{
transNum = value;
}
}
public string描述
{
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 =余额 - 借记;
return newBalance;
}
}
}

现在的代码为注册表单:

  public partial class注册:Form 
{
列表< Transaction> tranList = new List< Transaction>();

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

private void Register_Load(object sender,System.EventArgs e)
{
// regView表示我正在尝试使用$ b的Datagridview $ b regView.AutoSize = true;
regView.DataSource = tranList;
regView.Rows.Add(tranList [0]);
}
}

这里是我得到的输出。

解决方案

真的有两个高级的方法。



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



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.

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.

My question is, how do I go about getting the details of each of the objects in the list to display in the 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.

解决方案

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.

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) 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 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-Detailed-Data-Binding-Tutorial

这篇关于使用对象列表填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆