Windows 窗体上的 SubSonic 和 dateTimePicker 控件

发布于 2024-07-20 16:40:00 字数 488 浏览 9 评论 0原文

问题#1:最新工作版本

我目前使用的是 SubSonic 2.1built 491。 还有后期建设吗? 我在哪里可以得到它? 我知道 2.2 已发布,但它没有附带安装程序,我不知道如何修改 App.Config/Web.Config 来使用它。

问题 #2:Windows 窗体上的 dateTimePicker 控件存在问题。

我不断收到 System.FormatException 尝试从 SubSonic 检索数据到该控件或通过 SubSonic 将数据从该控件保存到数据库。

例如,如果我只想保存时间,我可以使用 .Text 属性。 要保存日期,我需要使用控件的 .Value 属性。

我尝试了各种转换,例如 Convert.ToDateTime(dateTimePicker.Value.ToString()) 等,但我找不到不会引发异常的一致模式。 对此的任何帮助将不胜感激。

Question #1: Latest working Version

I'm currently using SubSonic 2.1 built 491.
Is there a later build? Where can I get it?
I know 2.2 was released but it doesn't come with a Setup and I wouldn't know how to modify the App.Config/Web.Config to work with it.

Question #2: Issue with dateTimePicker control on Windows Form.

I keep getting System.FormatException trying to retrieve data From SubSonic to that control or saving data from that control to the Database through SubSonic.

For example, if I want to save the Time only, I can use the .Text property. To save the Date, I need to use the .Value property of the control.

I've tried all sorts of conversion such as Convert.ToDateTime(dateTimePicker.Value.ToString()) and others but I can't find a consistent pattern that would not throw an exception. Any help on this would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-07-27 16:40:01

关于问题 1 - 我认为 SS2.1 和 2.2 之间不需要任何 app.config / web.config 更改

对于问题 2 - 您是否有机会使用 MSSQL2008 中的新数据类型? 如果是这样,SS2.2 似乎还不能正确处理它们。

Re Question 1 - I don't think any app.config / web.config change is required between SS2.1 and 2.2

For Question 2 - are you using the new data types in MSSQL2008 by any chance? If so, SS2.2 doesn't seem to handle them properly yet.

装迷糊 2024-07-27 16:40:01

刚刚偶然发现了你的帖子。
我不确定这是否是 FormatException,但我也遇到了数据绑定 DateTimePicker 的异常。

问题是,

DateTimePicker.MinimumDateTime = #1/1/1753#

虽然

DateTime.MinValue = #1/1/0001#
New DateTime = #1/1#0001#

So 如果将属性绑定到返回早于 #1/1/1753# 或晚于 #12/31/9998# ( DateTimePicker.MaximumDateTime ) 的 DateTime 值的 DataGridView,则会出现异常。

我用我自己继承的 DateTimePicker 解决了这个问题(抱歉,但它是用 vb 编写的)

要使用它,您只需将 Subsonic 对象绑定到 value 属性即可。 但是您必须将 ShowCheckBox 属性设置为 true 并将布尔值绑定到 CheckedValue 属性(以指示日期已设置),该属性也保留在您的数据库中。

Now 如果返回空日期,则日期设置为 Now,checkedValue 设置为 false,从而导致 DateTimePicker 被禁用。
如果您选中复选框或使用日历选择日期,则复选框将设置为 true,并且复选框将被选中。

注意:不要直接绑定Checked属性,而是绑定CheckedValue属性。

Imports System.ComponentModel

Public Class MyDateTimePicker
    Inherits System.Windows.Forms.DateTimePicker

    <Bindable(True)> _
    Public Overloads Property Value() As DateTime
        Get
            If Not MyBase.Checked And (MyBase.Value < DateTimePicker.MinimumDateTime Or MyBase.Value > DateTimePicker.MaximumDateTime) Then
                Return DateTime.Now
            Else
                Return MyBase.Value
            End If
        End Get
        Set(ByVal value As DateTime)

            If ((value < DateTimePicker.MinimumDateTime Or value > DateTimePicker.MaximumDateTime) Or value = #1/1/1900#) Then
                MyBase.Value = DateTime.Now
                MyBase.Checked = False
            Else
                MyBase.Value = value
            End If

        End Set
    End Property

    Private _CheckedValue As Boolean

    <Bindable(True)> _
    Public Property CheckedValue() As Boolean
        Get
            Return _CheckedValue
        End Get
        Set(ByVal value As Boolean)
            _CheckedValue = value
        End Set
    End Property

    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)

        RefreshCheckedValue()
    End Sub

    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
        MyBase.OnKeyDown(e)

        RefreshCheckedValue()
    End Sub

    Private Sub RefreshCheckedValue()

        CheckedValue = Me.Checked

        If Not Me.DataBindings("CheckedValue") Is Nothing Then
            Me.DataBindings("CheckedValue").WriteValue()
        End If

    End Sub

    Protected Overrides Sub OnBindingContextChanged(ByVal e As System.EventArgs)
        MyBase.OnBindingContextChanged(e)

        Static checkedInitialized As Boolean
        If Not checkedInitialized AndAlso Not Me.DataBindings("CheckedValue") Is Nothing Then
            Me.Checked = Me.CheckedValue
            checkedInitialized = True
        End If

    End Sub

End Class

Just stumbled upon your post.
I'm not sure if It was a FormatException, but I got a Exception with a databound DateTimePicker, too.

The problem is that

DateTimePicker.MinimumDateTime = #1/1/1753#

while

DateTime.MinValue = #1/1/0001#
New DateTime = #1/1#0001#

So if bind a property to a DataGridView that returns a DateTime value earlier than #1/1/1753# or later then #12/31/9998# ( DateTimePicker.MaximumDateTime ) you get an exception.

I solved this with my own inherited DateTimePicker (sorry, but it's written in vb)

To use it you can just bind your Subsonic object to the value property. But you have to set the ShowCheckBox property to true and bind a bool value to the CheckedValue property (to indicate that the date is set) which is persisted in your db, too.

Now If an empty date is returned, the date is set to Now and the checkedValue to false, leading to a disabled DateTimePicker.
If you check the Checkbox or choose a date with the calender) the checkbox is set to true and the Checkbox is checked.

Attention: Do not bind the Checked property directly, bind the CheckedValue property.

Imports System.ComponentModel

Public Class MyDateTimePicker
    Inherits System.Windows.Forms.DateTimePicker

    <Bindable(True)> _
    Public Overloads Property Value() As DateTime
        Get
            If Not MyBase.Checked And (MyBase.Value < DateTimePicker.MinimumDateTime Or MyBase.Value > DateTimePicker.MaximumDateTime) Then
                Return DateTime.Now
            Else
                Return MyBase.Value
            End If
        End Get
        Set(ByVal value As DateTime)

            If ((value < DateTimePicker.MinimumDateTime Or value > DateTimePicker.MaximumDateTime) Or value = #1/1/1900#) Then
                MyBase.Value = DateTime.Now
                MyBase.Checked = False
            Else
                MyBase.Value = value
            End If

        End Set
    End Property

    Private _CheckedValue As Boolean

    <Bindable(True)> _
    Public Property CheckedValue() As Boolean
        Get
            Return _CheckedValue
        End Get
        Set(ByVal value As Boolean)
            _CheckedValue = value
        End Set
    End Property

    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)

        RefreshCheckedValue()
    End Sub

    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
        MyBase.OnKeyDown(e)

        RefreshCheckedValue()
    End Sub

    Private Sub RefreshCheckedValue()

        CheckedValue = Me.Checked

        If Not Me.DataBindings("CheckedValue") Is Nothing Then
            Me.DataBindings("CheckedValue").WriteValue()
        End If

    End Sub

    Protected Overrides Sub OnBindingContextChanged(ByVal e As System.EventArgs)
        MyBase.OnBindingContextChanged(e)

        Static checkedInitialized As Boolean
        If Not checkedInitialized AndAlso Not Me.DataBindings("CheckedValue") Is Nothing Then
            Me.Checked = Me.CheckedValue
            checkedInitialized = True
        End If

    End Sub

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文