Reputation: 7187
So I have a collection that I have in a listbox right now, which displays each item in the collection. I want to turn the listbox into a gridview, so I can add checkboxes and other drop downs. I am unable to find a tutorial that explains how to do this.
For the example my collection has 2 items, each item has multiple columns.
Right now my listbox uses this code
foreach (AMAPnr.Airplane airElement in AMAPnr.Airplanes)
{
lstPlanes.Items.Add(
"PassengerAmount: " + airElement.Passenger + " First Flight:" +
airElement.FirstFlight.ToString());
}
How would i go about changing this into a gridview?
Upvotes: 2
Views: 104
Reputation: 45789
Update: The OP has clarified that they chose the wrong tags and this was actually for WinForms.
If you add a DataGridView
to your form, and then put the following code in your forms codebehind, it works a treat:
private class Airplane
{
public string AirplaneName { get; set; }
public int PassengerAmt { get; set; }
public int FirstFlight { get; set; }
}
public Form1()
{
InitializeComponent();
var planes = new List<Airplane>();
planes.Add(new Airplane() { AirplaneName = "747 Jet", PassengerAmt = 417, FirstFlight = 1967 });
dataGridView1.DataSource = planes;
}
I've used a custom Airplane class to show this example a I don't know precisely how your code's structured and it was easier for me that way. You should be able to plug in your custom datatype relatively easily.
Upvotes: 2
Reputation: 25834
public IEnumerable<AMAPnr.Airplane> getItems(Planes)
{
foreach (AMAPnr.Airplane airElement in Planes)
{
yield return airElement;
}
yield break;
}
Then just do myDataGrid.DataSource = getItems(AMAPnr.Airplanes);
You can also just do myDataGrid.DataSource = lstPlanes;
Upvotes: 1