To display data in a tabular form, use
DataGridView[
^] control.
You'll need to make some changes in your code:
1. Add new class:
public class Föremål
{
public string Kurs { get; set; }
public string Lärare { get; set; }
public Föremål(string _kurs, string _lärare)
{
Kurs = _kurs;
Lärare = _lärare;
}
}
2. Add DataGridView control on your form and change it name to:
DgvFöremåls
3. Then in your form:
public partial class Form1 : Form
{
List<Föremål> föremåls = new List<Föremål>();
BindingSource bs = new BindingSource();
public Form1()
{
InitializeComponent();
}
private void buttonSpara_Click(object sender, EventArgs e)
{
Föremål f = new Föremål(textBoxKurs.Text, textBoxLärare.Text)
föremåls.Add(f);
textBoxKurs.Text = string.Empty;
textBoxLärare.Text = string.Empty;
textBoxKurs.Focus();
bs.DataSource = föremåls;
DgvFöremåls.DataSource = bs;
}
}
For further details, please see:
Bind data to DataGridView Control - Windows Forms | Microsoft Docs[
^]