我在 C# Visual Studio GUI 的窗体上的组合框中显示多个值,我正在使用下面的代码执行此操作。通过连接多个列名称,我可以显示多个值。start_date 表示数据类型为 datetime 的列,我需要将其显示为 dd/mm/yyyy。如何在表单上将日期时间显示为日期?
trip_DateTableAdapter1.Fill(this.outdoorParadiseDataSet1.Trip_Date);
DataTable dt = this.outdoorParadiseDataSet1.Trip_Date;
dt.Columns.Add("ConcatenatedField", typeof(string), "id + ' : ' + start_date");
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "ConcatenatedField";
你可以试试 Combobox 的Format
事件
private void Form1_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("id", typeof(string));
table.Columns.Add("start_date", typeof(DateTime));
table.Columns.Add("DateField", typeof(string), "id + ' : ' + start_date");
DateTime test = new DateTime(2018, 03, 20);
table.Rows.Add("123", test);
comboBox1.DataSource = table;
comboBox1.DisplayMember = "DateField";
// tried to make your datasource here.
}
private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
{
DateTime date = (DateTime)((DataRowView)e.ListItem).Row["start_date"];
//found start_date in itemarray.
string id = ((DataRowView)e.ListItem).Row["id"].ToString();
// found id in item array
string formatValue = date.AddDays(11).ToShortDateString();
// i added also 11 days to see if the actual date is changing
e.Value = id + " : " + DateTime.ParseExact(formatValue, "dd/MM/yyyy", new CultureInfo("tr-TR"));
// then combined both values, and formatted the datetime as you desired.
}
希望有所帮助,
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句