使用对象列表填充datagridview

mor*_*295 10 c# datagridview winforms

我有一个包含一系列事务对象的List.我想要做的是在加载表单时在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;
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在"注册"表单的代码:

    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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出. 注册输出

Gje*_*ema 9

这有两种高级方法.

1)直接将手动创建的行添加到DataGridView.在这种情况下,您必须在更改时手动更新/删除它们.如果您在初始化后不打算更改/更改显示内容,则此方法"正常".如果你这样做就变得站不住脚了.

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

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

CodeProject有一篇不错的文章让你开始沿着这条路走下去,但快速的谷歌搜索将会产生许多其他文章.

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


小智 5

用作DGV:

DataGridView groupListDataGridView;
Run Code Online (Sandbox Code Playgroud)

柱:

DataGridViewTextBoxColumn groupListNameColumn;
Run Code Online (Sandbox Code Playgroud)

列设置应该是这样的:

groupListNameColumn.DataPropertyName = "name";
Run Code Online (Sandbox Code Playgroud)

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

groupListDataGridView.AutoGenerateColumns = false;
Run Code Online (Sandbox Code Playgroud)

像这样填充:

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();
}
Run Code Online (Sandbox Code Playgroud)

和型号:

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
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是SortableBindingList <>()? (5认同)

小智 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;
Run Code Online (Sandbox Code Playgroud)