Populating WinForms Controls at Runtime with C#


Display a Winforms ListView Vertically at Runtime Using C#

A C# ListView can display row data vertically, as well as horizontally, like this:

A Sample C# ListView Displaying Row Data Vertically

h3>A Sample C# ListView Displaying Row Data Vertically

The example method below takes the first row of a DataTable and displays it vertically:

public void FillListViewVertically(ListView listView, DataTable dataTable) {
  listView.Columns.Add("Field");
  listView.Columns.Add("Value");
  
  foreach(DataColumn dataColumn in dataTable.Columns) {
    ListViewItem listViewItem = listView.Items.Add(dataColumn.ColumnName);
    listViewItem.SubItems.Add(dataTable.Rows[0][dataColumn.ColumnName].ToString());
}

When using this method in your code provide the name of your ListView and your DataTable in the method's arguments, like this:

FillListViewVertically(<yourListView>, <yourDataTable>);

Next Topic