排序自定义日期时间C#
问题描述:
我需要帮助按降序排序自定义日期时间在列表框中。排序自定义日期时间C#
我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/mm/yyyy HH:mm";
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text + ", " + Convert.ToDateTime(this.dateTimePicker1.Value).ToString("dd/mm/yyyy HH:mm"));
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateTimePicker1.CustomFormat = "dd/mm/yyyy HH:mm";
}
}
}
我添加从textBox1的日期和时间从DateTimePicker1文字,我想它根据时间自动排序。
谢谢!
答
我一直保持为目的List<string>
:
试试这个:
List<string> lst;
public Form2()
{
InitializeComponent();
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/mm/yyyy HH:mm";
lst = new List<string>();
}
private void button2_Click(object sender, EventArgs e)
{
lst.Remove(listBox1.SelectedItem.ToString());
BindList();
}
private void button1_Click(object sender, EventArgs e)
{
string s = textBox1.Text + ", " + Convert.ToDateTime(this.dateTimePicker1.Value).ToString("dd/mm/yyyy HH:mm");
lst.Add(s);
BindList();
}
private void BindList()
{
lst = (lst.OrderByDescending(s => s.Substring(s.LastIndexOf(" "), s.Length - s.LastIndexOf(" ")))).ToList();
listBox1.DataSource = lst;
}
+0
作品,感谢的人:) – user3215507
您如何更改时间?我想当你运行你的程序时,它会从系统的日期时间中获得时间,而且时间永远不会改变。 – Hassan
时间来自dateTimePicker1,我想排序这些 – user3215507