0

I am making a simple windows forms application. In my listview i want to display three things; date, time and description. I am now doing it like this:

task = date + time + description;

(task, date, time, description are all strings).

which give the result:

enter image description here

But what i want is to make these strings to display below the different categories, something like this:

enter image description here

How would go about formatting the 3 strings (date,time,desciption) into one (task) so that they display as in picture two?

2
  • 4
    Is that a normal TextBox control? A ListView with 3 columns might be better and easier to format. Commented Apr 4, 2016 at 18:33
  • How are you actually displaying the bound data? It looks like task is a single string. But if you have three separate values and want to display them in column/tabular format, then it would make more sense to create an object containing those three values and bind a ListView or GridView or something like that to a collection of those objects. Commented Apr 4, 2016 at 18:36

3 Answers 3

1

Here's a bit of an expanded example for you.

While you may find a way to concatenate the strings and then space them properly by displaying them in something other than a listview, a listview is going to be your best option.

Your code will look something akin to this:

public partial class MainWindow : Window
{

    //Your data
    private string date = "2016-04-04";
    private string time = "20:20";
    private string description = "poop";

    //Declare a list
    public List<object> myList { get; set; }


    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        //Instantiate your list
        myList = new List<object>();

        //Make a new object
        var listObject = new
        {
            newDate = date,
            newTime = time,
            newDescription = description
        };

        //Add that object to your list
        myList.Add(listObject);
    }
}

And your XAML to display your results:

<Window x:Class="WpfApplicationTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView x:Name="listView" ItemsSource="{Binding myList}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Date" DisplayMemberBinding="{Binding newDate}"/>
                    <GridViewColumn Header="Time" DisplayMemberBinding="{Binding newTime}"/>
                    <GridViewColumn Header="Description" DisplayMemberBinding="{Binding newDescription}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>
Sign up to request clarification or add additional context in comments.

Comments

1

Concatenating items with + will just perform normal string concatenation, which is why you get everything in a single lump.

To do what you ask you have to add a new ListViewItem where you specify a string array in the constructor. Each array element represents a column in the ListView: {column 1, column 2, column 3, etc...}

For example:

ListView1.Items.Add(new ListViewItem(new string[] {date, time, description}));

MSDN Documentation for the ListViewItem(string[]) constructor: https://msdn.microsoft.com/en-us/library/faw83h4f(v=vs.110).aspx

Comments

1

You need to set View:Details from ListView properties to give a grid-style look. When you create a ListItem, it is treated as the data of first column and subsequent SubItems are treated as extra columns for that particular ListItem. Try this example.

ListViewItem lvi;
for (int i = 0; i < 10; i++)
{
   lvi = new ListViewItem();
   lvi.Text = "first column";
   lvi.SubItems.Add("second column");
   lvi.SubItems.Add("third column");
   listView1.Items.Add(lvi); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.