Populating a datagridview with a list of objects

I have a List containing a number of transaction objects. What I'm trying to do is display these transaction objects in the Datagridview control when the form loads, basically Datagridview needs to represent something from the transaction register to display data for each of the transaction objects in the list.

I have to admit a lack of experience when it comes to using Datagridviews, and I have difficulty understanding what I need to do here.

My question is: how do I get information about each of the objects in the list that will be displayed in Datagridview?

Here is my code.

First 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 of the "Registration" 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 is the result that I get. Register output

+8
c # winforms datagridview
source share
2 answers

There are actually two high-level approaches.

1) Add manually created rows directly to the DataGridView . In this case, you must manually update / delete them as things change. This approach is "good" if you are not going to change / modify the contents of the display after it is initialized. It becomes untenable if you do it.

To add it directly, you need to create a DataGridViewRow and populate it with individual values, and then add a DataGridViewRow to DataGridView.Rows .

2) Data bind DGV. There are many articles on binding data to a DataGridView . In some cases, it's easier to just add your data to the DataTable , and then extract the DataView from it and bind the DataGridView to the DataView . Other people find it easier to directly attach to a collection.

CodeProject has a decent article to get you started on this path, but a quick Google search will yield many other articles.

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

+9
source share

use as DGV:

 DataGridView groupListDataGridView; 

column:

 DataGridViewTextBoxColumn groupListNameColumn; 

the column setting should be like this:

 groupListNameColumn.DataPropertyName = "name"; 

use this property, otherwise all columns will be added.

 groupListDataGridView.AutoGenerateColumns = false; 

filled as follows:

 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 } 
+4
source share

All Articles