I have data consisting of two arrays, string item [], and string location [],

How do you create a DataGridView ( or is that even the right component to use) to
display the data in a windows form, like in a listbox?

Basically I need to write a method to display two columns of data in a DataGridView that is coming out of a for loop, with two string variables.

Any help would be appreciated.


Thanks.


Jim

Recommended Answers

All 6 Replies

The easiest way is to store the arrays as properties of a class, then set the data source of your DataGridView to an instance of the class and add columns with the DataPropertyName set to the two properties that you created.

Example:

public class MyContainer
{
  public string[] Items
  {
    get;
    set;
  }

  public string[] Locations
  {
    get;
    set;
  }
}

public class MyForm: Form
{
  public MyForm()
  {
    InitializeComponents();
    MyContainer container = new MyContainer();
    container.Items = InitialiseItems();
    container.Locations = InitialiseLocations();
    myDataGridView.DataSource = container;
    myDataGridView.Columns[0].HeaderText = "Item";
    myDataGridView.Columns[0].DataPropertyName = "Items";
    myDataGridView.Columns[1].HeaderText = "Location";
    myDataGridView.Columns[1].DataPropertyName = "Locations";
  }
}

Instead of creating a new class and methods to fill the class we can use LINQ to do that for us:

var allItems = item.Zip(location, (i, l) => new {Item = i, Location = l});
commented: Wow, didn't know you could do that, thanks for the tip :) +5

create a combobox column in datagridview and pass the string array to the column as datasource

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
            column.DataPropertyName =Column_Name ;
            column.HeaderText = Column_Name;
            column.Name = Column_Name;

and then for one cell

((DataGridViewComboBoxCell)dg.Rows[r].Cells["Column_Name"]).DataSource = string_Array;

Thanks for all your answers! On one hand C# is fairly easy to learn the basics of, and on the other, is so extensive, that there is always a different way to do things.

I ended up not using a datagridview, and accessed the info in the listbox by index only, and the program was simple enough that it worked well enough. The datagrid would have been a more elegant solution, and I DO need to learn how to use it so I'll be doing some coding till I feel confident with the intricacies of the examples you provided.

Thanks again for taking the time.

Jim

Instead of creating a new class and methods to fill the class we can use LINQ to do that for us:

var allItems = item.Zip(location, (i, l) => new {Item = i, Location = l});

Interesting...
but there is some code missing. Would you mind showing it all? I mean, where from is "item" and "Zip"? Is Zip methodonly in VS2010, cause I dont have it it VS 2008.

Interesting...
but there is some code missing. Would you mind showing it all? I mean, where from is "item" and "Zip"? Is Zip methodonly in VS2010, cause I dont have it it VS 2008.

item and location are the names of his arrays (from the first post). Zip is a new (as of 4.0) LINQ extension method that allows you to take two IEnumerables and combine them to produce one output IEnumerable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.