Reputation: 41
I'm very new to programming so apologies if this is a very obvious question. Ive searched online but not found an answer that quite fits yet.
I am writing a programme that interprets bytes from an index within a filestream. Once they have been converted into human readable dates/strings/ints etc I wanted to use a Winform to display the results in columns. Currently I using a listbox and just dispalying each entry seperated by columns but this feels lile a very clunky way of doing it.
Can someone please suggest how I might go about placing the results into a display that uses columns?
Upvotes: 0
Views: 595
Reputation: 14432
It's better to use a ListView
than a ListBox
in your case. Here's an example showing all words in a string in separate columns in a ListView:
Make sure following property is set to your ListView (here the Name is ColumnsListView):
ColumnsListView.View = View.Details;
This method takes a string, splits it by space and adds a column for each of the values:
private void SetListView(string input)
{
var values = input.Split(' ');
ColumnsListView.Columns.Add("Column1");
var item = new ListViewItem(values[0]);
for (var i = 1; i < values.Length; i++)
{
ColumnsListView.Columns.Add("Column" + (i+1));
item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = values[i] });
}
ColumnsListView.Items.Add(item);
}
This can be done differently when using LinQ's Skip()
method to add the item with subitems:
private void SetListView(string input)
{
var values = input.Split(' ');
for (var i = 0; i < values.Length; i++)
ColumnsListView.Columns.Add("Column" + (i + 1));
var item = new ListViewItem(values[0]);
item.SubItems.AddRange(values.Skip(1).ToArray());
ColumnsListView.Items.Add(item);
}
Update:
Here's an example in case you want to use a DataGridView
:
private void SetDataGridView(string input)
{
var values = input.Split(' ');
for (var i = 0; i < values.Length; i++)
ColumnsDataGridView.Columns.Add("Column" + (i + 1), "Column" + (i + 1));
ColumnsDataGridView.Rows.Add(values);
}
Upvotes: 1