Show date field value in a listview in a C #

-3

Good evening, everyone. I need to pull values from a data type field (MySQL) and display it in a listview in a C # application. It turns out that the listview shows the value with date and time, but I need only the date to be displayed. How do I do? Thanks!

    
asked by anonymous 11.09.2017 / 01:31

1 answer

2

At the time of adding the item to listview you should format the date as you wish, otherwise the result will be equal to the ToString() method that results in Date and Time.

To format the date you have several options, in your case to display only the date, you can do this:

Consider data as the variable of type DateTime

1 = data.ToShortDateString();

2 = data.ToString("d");

3 = data.ToString("dd/MM/yyyy");

For more formats see the DateTime.ToString() documentation:

link

See in the Fiddle .NET: link

Considering your current code that you put in the comments:

ListViewItem CAMPOS = new ListViewItem(READER[0].ToString()); 
CAMPOS.SubItems.Add(READER[1].ToString()); 
CAMPOS.SubItems.Add(READER[2].ToString()); 
CAMPOS.SubItems.Add(READER[3].ToString()); 
CAMPOS.SubItems.Add(READER[4].ToString());

And considering, for example, that your date column is the third column, index 2, you can do this:

ListViewItem CAMPOS = new ListViewItem(READER[0].ToString()); 
CAMPOS.SubItems.Add(READER[1].ToString()); 
CAMPOS.SubItems.Add(READER.GetDateTime(2).ToString("dd/MM/yyyy")); 
CAMPOS.SubItems.Add(READER[3].ToString()); 
CAMPOS.SubItems.Add(READER[4].ToString());

I recommend that you read about naming patterns as well.

    
11.09.2017 / 04:10