Reputation: 41
I have a Day- and Timepicker on my form that is supposed to only show me the time in this format:
01-May-13.
I've set the custom format in the properties to: dd/mm/yyyy but the value still shows the time when I load the data in a listbox (link).
This is the code I am using to display the data from my database:
private void loadlist() // methode om de lijsten te laden (SELECT*)
{
listBox1.Items.Clear(); // Maakt listBox1 leeg
listBox2.Items.Clear(); // Maakt listBox2 leeg
listBox3.Items.Clear(); // Maakt listBox3 leeg
listBox4.Items.Clear(); // Maakt listBox4 leeg
cn.Open(); // DB connectie openen
cmd.CommandText = "SELECT * FROM tafel"; // Query
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
listBox1.Items.Add(dr[1].ToString()); // Voegt rows uit tbl in listbox
listBox2.Items.Add(dr[2].ToString()); // en zet ze om in string
listBox3.Items.Add(dr[3].ToString());
listBox4.Items.Add(dr[4].ToString());
}
}
cn.Close();
}
My current format is: dd/MM/yyyy (and it shows: 10-Apr-2013 17:08 in the listBox)
I'm sorry if this is an easy question.. I couldn't find out how to do it.
Upvotes: 0
Views: 408
Reputation: 64
Convert.ToDateTime(dr[1].ToString()).ToString("yyyy/MM/dd");
add this one
Upvotes: -1
Reputation: 13877
A couple different things to try.
string dateOnly = dateTimeVariable.ToString("dd-MM-yyyy");
or
dateTimeVariable.ToShortDateString();
Both should give you what you're looking for.
EDIT: From your code, try this now:
DateTime.Parse(dr[1]).ToShortDateString());
Upvotes: 2