TimeSpan 的格式字符串,仅当天数不为零时才将天数添加到字符串中
Format string for a TimeSpan that adds the days to the string only if they are non-zero
我需要设置 TimeSpan 的格式,以便只有非零时才显示天数。
现在我正在使用 @"d' days, 'hh\:mm"
,它将 new TimeSpan(1,2,3,4,5)
格式化为 1 days, 02:03
,但它也会将 new TimeSpan(0,2,3,4,5)
格式化为 0 days, 02:03
,这不是我想要的 - 在这种情况下我只想要 02:03
。
问题是完全相同的格式将应用于所有时间跨度,这是我的问题和 display timespan nicely 之间的区别,所以我不能像这样使用(伪)代码:
if (hasDays) {
format = @"dd' days, 'hh\:mm";
} else {
format = @"hh\:mm";
}
我不能使用它,条件必须以某种方式编码在格式字符串本身中。有可能实现吗?
请注意:我对不涉及使用完全相同格式字符串的解决方案不感兴趣。如果不可能,那么说明这样的答案就足够了。
我认为您只能在 DataGridView.CellFormatting
这样的事件中执行此操作。您可以在此处为您的单元格设置单独的格式。
无法使用格式字符串进行条件格式化;但根据显示数据的控件,您可以使用一些解决方案来执行条件格式。
在 Windows 表单中,ListBox
、ComboBox
、DataGridView
和 DataGrid
支持复杂的数据绑定。在此 post 中,我将展示如何在 Windows Forms 应用程序中为这些控件执行条件格式设置。
列表框和组合框
将FormattingEnabled
设置为true
并处理Format
事件。使用 e.Value
获取项目值并设置格式化值。
DataGridView
处理 CellFormatting
事件。您可以使用 e.Value
获取单元格值或设置格式化值。
DataGrid
您可以基于 DataGridTextBoxColumn
创建自定义 DataGridColumnStyle
并覆盖 GetColumnValueAtRow
和 return 格式的值。
例子
在以下所有示例中,我使用一种方法来格式化值:
string FormatValue(object value)
{
if (value is TimeSpan)
{
var time = (TimeSpan)value;
if (time.TotalDays < 1.0)
return time.ToString(@"hh\:mm");
else
return time.ToString(@"d' days, 'hh\:mm");
}
return string.Format("{0}", value);
}
列表框和组合框
void listBox_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = FormatValue(e.Value);
}
DataGridView
void dg_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.Value = FormatValue(e.Value);
}
DataGrid
//public class MyDataGridTextBoxColumn : DataGridTextBoxColumn
protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum)
{
var value = base.GetColumnValueAtRow(source, rowNum);
return FormatValue(value);
}
您应该为要使用此格式的栏使用此栏样式。
我需要设置 TimeSpan 的格式,以便只有非零时才显示天数。
现在我正在使用 @"d' days, 'hh\:mm"
,它将 new TimeSpan(1,2,3,4,5)
格式化为 1 days, 02:03
,但它也会将 new TimeSpan(0,2,3,4,5)
格式化为 0 days, 02:03
,这不是我想要的 - 在这种情况下我只想要 02:03
。
问题是完全相同的格式将应用于所有时间跨度,这是我的问题和 display timespan nicely 之间的区别,所以我不能像这样使用(伪)代码:
if (hasDays) {
format = @"dd' days, 'hh\:mm";
} else {
format = @"hh\:mm";
}
我不能使用它,条件必须以某种方式编码在格式字符串本身中。有可能实现吗?
请注意:我对不涉及使用完全相同格式字符串的解决方案不感兴趣。如果不可能,那么说明这样的答案就足够了。
我认为您只能在 DataGridView.CellFormatting
这样的事件中执行此操作。您可以在此处为您的单元格设置单独的格式。
无法使用格式字符串进行条件格式化;但根据显示数据的控件,您可以使用一些解决方案来执行条件格式。
在 Windows 表单中,ListBox
、ComboBox
、DataGridView
和 DataGrid
支持复杂的数据绑定。在此 post 中,我将展示如何在 Windows Forms 应用程序中为这些控件执行条件格式设置。
列表框和组合框
将
FormattingEnabled
设置为true
并处理Format
事件。使用e.Value
获取项目值并设置格式化值。DataGridView
处理
CellFormatting
事件。您可以使用e.Value
获取单元格值或设置格式化值。DataGrid
您可以基于
DataGridTextBoxColumn
创建自定义DataGridColumnStyle
并覆盖GetColumnValueAtRow
和 return 格式的值。
例子
在以下所有示例中,我使用一种方法来格式化值:
string FormatValue(object value)
{
if (value is TimeSpan)
{
var time = (TimeSpan)value;
if (time.TotalDays < 1.0)
return time.ToString(@"hh\:mm");
else
return time.ToString(@"d' days, 'hh\:mm");
}
return string.Format("{0}", value);
}
列表框和组合框
void listBox_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = FormatValue(e.Value);
}
DataGridView
void dg_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.Value = FormatValue(e.Value);
}
DataGrid
//public class MyDataGridTextBoxColumn : DataGridTextBoxColumn
protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum)
{
var value = base.GetColumnValueAtRow(source, rowNum);
return FormatValue(value);
}
您应该为要使用此格式的栏使用此栏样式。