Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Adding existing items to a datagridview in visual basic

Hi all,

I've looked everywhere to try and find this but have had no luck. What I'm trying to do is add items from a list that I have already created. Each item has an itemcode (string), an itemdescription (string), an item count (integer) and onorder (bool).

I have assigned values to each of these as well. So, for example, i have a train with an itemcode a0001, description as train, count as 5, on order as false.

So i want to take all of the data of these items and put it into a datagridview.

2 Answers

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey Cameron,

When I've added items to a DataGridView from a List, I usually

1) add the necessary number of columns 2) add the necessary number of rows 2) loop through each row and add each item's properties to each column

Something like this:

public DataGridView addListItemsToDataGridView(List<Items> itemList) 
{
    DataGridView dgv = new DataGridView();

    //add columns to DataGridView
    dgv.Columns.Add("Code", "Code");
    dgv.Columns.Add("Description", "Description");
    dgv.Columns.Add("Count", "Count");
    dgv.Columns.Add("onOrder", "onOrder");

    //add rows to DataGridView
    for (int i = 0; i < itemList.Count - 1; i++) 
    {
        dgv.Rows.Add();
    }

    //populate data in the cells you created in the previous steps
    int r = 0;
    foreach (Items item in itemList) 
    {
        dgv[0, r].Value = item.code;
        dgv[1, r].Value = item.description;
        dgv[2, r].Value = item.count;
        dgv[3, r].Value = item.onOrder;
        r++;
    }

    return dgv;
}

You can also look at links like this: https://stackoverflow.com/questions/6092463/how-can-i-manually-add-data-to-a-datagridview

Thanks for your help. I have found an easier way, however (partially by fluke and partially by watching another video). What I have done is gone to my grid view properties -> datasource and added a binding source to my items class. Then, in the forms class, I have added the following code;

Public Class Data //Data is the name of my form Private _source As New DataSource() Private Sub Data_Load(sender As Object, e As EventArgs) Handles MyBase.Load

ItemGridView.DataSource = _source.GetItems //GetItems is the name of an Ilist that is derived from my another list that contains all of my items

Hope this makes sense. I'm a complete newby to all of this and your help is greatly appreciated.