Winforms 格式化datagridview列中的datetimepicker

Winforms 格式化datagridview列中的datetimepicker,winforms,datagridview,customcolumn,Winforms,Datagridview,Customcolumn,我试图使用DateTime picker自定义gridview列类型,因为我希望以24小时格式显示小时和分钟,而不显示秒或am PM指示器 我已将EditingControlFormattedValue设置为“HH:mm”,并且在不实际编辑时,该值会正确显示 编辑时,如果在CalendarEditingControl的构造函数中我将编辑控件设置为CustomFormat=“HH:mm”,则该控件将显示周和月的日期。(!?) 当我改为使用Format=DateTimePickerFormat.Ti

我试图使用DateTime picker自定义gridview列类型,因为我希望以24小时格式显示小时和分钟,而不显示秒或am PM指示器

我已将EditingControlFormattedValue设置为“HH:mm”,并且在不实际编辑时,该值会正确显示

编辑时,如果在CalendarEditingControl的构造函数中我将编辑控件设置为CustomFormat=“HH:mm”,则该控件将显示周和月的日期。(!?)

当我改为使用Format=DateTimePickerFormat.Time时,控件在编辑时显示AM或PM


如何说服此控件只显示我关心的部分DateTime值?(C#,VS 2008)

要使链接代码按您想要的方式工作,您需要做一些调整:

  • 注释掉CalendarCell()构造函数中硬编码的行(
    this.Style.Format=“d”

  • 告诉CalendarEditingControl使用自定义的指定格式:

  • 在设计器中,设置所需的格式(EditColumns->DefaultCellStyle->format)


我发现我需要做以下更改:

在CalendarCell的构造函数中,将格式更改为24小时

public CalendarCell()
    : base()
{
    // Use the 24hr format.
     //this.Style.Format = "d";
     this.Style.Format = "HH:mm";
}
在编辑控件的构造函数中,指定使用自定义格式。我还随意设置了
showup
true,以便在编辑单元格时不显示日历图标:

public CalendarEditingControl()
{
    //this.Format = DateTimePickerFormat.Short;
    this.Format = DateTimePickerFormat.Custom;
    this.CustomFormat = "HH:mm";
    this.ShowUpDown = true;
}
更改EditingControlFormattedValue。这看起来并不是必须的,但感觉像是要离开

// Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
// property.
public object EditingControlFormattedValue
{
    get
    {
        //return this.Value.ToShortDateString();
        return this.Value.ToString("HH:mm");
    }
    set
    {
        if (value is String)
        {
            try
            {
                // This will throw an exception of the string is 
                // null, empty, or not in the format of a date.
                this.Value = DateTime.Parse((String)value);
            }
            catch
            {
                // In the case of an exception, just use the 
                // default value so we're not left with a null
                // value.
                this.Value = DateTime.Now;
            }
        }
    }
}

这是可行的,但它对特定的格式进行了硬编码。如果使用
cellStyle.Format
,则设计器中指定的内容将贯穿整个控件。(见我的答案举例)我愿意接受这两个答案,因为它们都能解决问题。我同意你的答案,因为它实际上更灵活,而且是第一个,即使在这种情况下硬编码格式也可以。你的答案比我的好。为了完整起见,我建议添加代码以编程方式设置默认单元格样式,也可以通过设计器进行设置。除了ApplyCellStyleToEditingControl方法中的if语句之外,想不出一个好的方法来使用决战调整。@Mickey如果想让你喜欢这两种方法的话,那就是向上投票——无论如何,我总是向上投票一个被接受的答案。但我可能很快就会删除我的答案,特别是如果John包括我建议的编辑。John只是随意编辑,所以使用项目符号而不是句号。对不起,如果你不想那样做,请后退。
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
// property.
public object EditingControlFormattedValue
{
    get
    {
        //return this.Value.ToShortDateString();
        return this.Value.ToString("HH:mm");
    }
    set
    {
        if (value is String)
        {
            try
            {
                // This will throw an exception of the string is 
                // null, empty, or not in the format of a date.
                this.Value = DateTime.Parse((String)value);
            }
            catch
            {
                // In the case of an exception, just use the 
                // default value so we're not left with a null
                // value.
                this.Value = DateTime.Now;
            }
        }
    }
}