Show date field value in a listview in a C#application

Asked

Viewed 299 times

-3

Good evening everyone. I need to pull values from a date (Mysql) field and display in a listview in a C#application. It turns out that listview shows the value with date and time, but I need to only display the date. How do I? Thank you!

  • Show what you’ve done, your code.

  • 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());

  • Just to explain, the last field is the person’s date of birth. This routine is within a while, to show all the records. There in the Listview column, where only the date of birth should appear, the date followed by the time (always 00:00:00)

  • To save the date in the database, I am using this routine to take the value in a Maskedtextbox and write: string Datan = Convert.Todatetime(MSKDATA.Text). Tostring("yyyy-MM-dd"); It is recorded as yyyy-MM-dd, but when recovering in a query, the time comes together.

1 answer

2

When adding the item to listview you must format the date as you want, otherwise the result will be equal to the method ToString() resulting in Date and Time.

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

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 documentation of DateTime.ToString():

https://msdn.microsoft.com/pt-br/library/zdtaw1bw%28v=vs.110%29.aspx? f=255&Mspperror=-2147217396

Check . NET Fiddle: https://dotnetfiddle.net/60syZo

Considering your current code 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 so:

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 you read about nomenclature patterns as well.

  • Turns out the value comes from a date field, straight to a Listview. I created an object of type Mysqldatareader, then picked column by column and assigned to a column of Listview. Only this value coming from a DATE type field in Mysql goes with the date and time

  • To save the date in the database, I am using this routine to take the value in a Maskedtextbox and write: string Datan = Convert.Todatetime(MSKDATA.Text). Tostring("yyyy-MM-dd"); It is recorded as yyyy-MM-dd, but when recovering in a query, the time comes together.

  • put the code into which fills your listview

  • edited the answer

  • I’m going to try this solution. As for the naming patterns, I didn’t use it because this project is only for testing. I appreciate the help and I’ll get back to you as soon as I can.

  • The solution you gave me worked. Thank you. I also thank the others who helped.

  • do not forget to mark as reply. Thank you

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.