【问题标题】:format datetimepicker in datagridview column在 datagridview 列中格式化 datetimepicker
【发布时间】:2012-05-14 19:33:04
【问题描述】:

我正在尝试使用来自 this MSDN example on How to host controls in DataGridViewCells. 的 DateTime 选择器自定义 gridview 列类型我想以 24 小时格式显示小时和分钟,不带秒或 AM PM 指示器。

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

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

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

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

【问题讨论】:

    标签: winforms datagridview customcolumn


    【解决方案1】:

    您需要对链接代码进行一些调整才能使其按照您想要的方式工作:

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

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

    • 在设计器中,设置你想要的格式(EditColumns->DefaultCellStyle->Format)

      public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
      {
          this.Format = DateTimePickerFormat.Custom;
          this.CustomFormat = dataGridViewCellStyle.Format;
          // ... other stuff
      }
      

    【讨论】:

    • 我愿意接受这两个答案,因为它们都解决了问题。我同意你的答案,因为它实际上更灵活一些,并且是第一个,即使在这种特殊情况下硬编码格式会很好。
    • 你的答案比我的好。我是否建议添加代码以编程方式设置默认单元格样式以及通过设计器,只是为了完整性。除了 ApplyCellStyleToEditingControl 方法中的 if 语句之外,想不出使用 ShowUpDown 调整的好方法。
    • @mickeyf 向您展示两者都喜欢的方式是投票 - 无论如何,我总是赞成一个接受的答案。但我可能很快就会删除我的答案,特别是如果约翰包括我建议的编辑。
    • 约翰,只是冒昧编辑,所以使用项目符号而不是句号。抱歉,如果您不希望这样,请回滚。
    【解决方案2】:

    我发现我需要进行以下更改:

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

    public CalendarCell()
        : base()
    {
        // Use the 24hr format.
         //this.Style.Format = "d";
         this.Style.Format = "HH:mm";
    }
    

    在编辑控件的构造函数中指定使用自定义格式。我还冒昧地将 ShowUpDown 设置为 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,那么设计器中指定的任何内容都会贯穿整个控件。 (请参阅我的答案示例)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多