0

我目前正在使用VB。我想做一个日历控件,其中突出显示/选择了日期。所有这些日期都是从数据库中检索的。

我需要知道的第一件事是如何将所有日期放入数组中我需要知道的第二件事是如何突出显示数组中的所有日期。

我在互联网上做了一些研究,他们说了一些关于 selectedDates 和 selectedDates 集合和 dayrender 的内容。但坦率地说,我真的找不到任何关于此的 VB 代码。日期格式为 dd/MM/yyyy

    Imports System.Data.SqlClient

    Partial Class _Default
Inherits System.Web.UI.Page

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim connectionString As String = ConfigurationManager.ConnectionStrings("CleanOneConnectionString").ConnectionString
    Dim connection As SqlConnection = New SqlConnection(connectionString)
    connection.Open()
    Dim sql As String = "Select schedule From OrderDetails Where schedule is not null"
    Dim command As SqlCommand = New SqlCommand(sql, connection)
    Dim reader As SqlDataReader = command.ExecuteReader()
    If (reader.Read()) Then
        If (reader.HasRows) Then
            While reader.Read()
                myCalendar.SelectedDates.Add(CType(reader.GetDateTime(0), Date))
            End While
        End If
    End If
    reader.Close()
    connection.Close()
    myCalendar.SelectedDayStyle.BackColor = System.Drawing.Color.Red
End Sub
    End Class

我的日历

    <asp:Calendar ID="myCalendar" runat="server" ShowGridLines="True">
</asp:Calendar>

更新了我所做的,但仍然没有显示感谢您的帮助

4

2 回答 2

2

对于您问题的第二部分,您可以在这篇文章中看到如何突出显示指定的日期,尽管使用的是 C# 语法。

我将假设现在使用 L2S 格式来获取日期(如果您需要更好的细节,请与实际实现进行评论)。

我建议您将要选择的日期保存在表单上的变量中(而不是限定为函数),以防止每次呈现一天时都运行数据库查询。考虑到这一点,这里有一些示例代码(免费,所以请原谅和评论基本/麻烦的语法问题):

Private DatesToHighlight As IEnumerable(Of Date)

' implementation details provided so commented this bit out, see EDIT below
'Protected Sub PopulateDatesToHighlight()
'    DatesToHighlight = db.SomeTable.Select(Function(n) n.MyDateField)
'End Sub

Protected Sub DayRenderer(ByVal object As Sender, ByVal e As DayRenderEventArgs)
   If DatesToHighlight.Contains(e.Day.Date) Then
       e.Cell.BackColor = System.Drawing.Color.Red
   End If
End Sub

如我链接的问题中所述,您需要更改日历控件的标记以提供ondayrender像这样的参数 ondayrender="DayRenderer"

关于将日期更改为数组,这取决于它们一开始的格式。如果在继承自 IEnumerable 的任何内容中,则可以使用ToArray()。如果它们只是变量,您可以使用日期初始化数组

Dim myDates() As Date = {dateVar1, dateVar2}

希望有帮助吗?

编辑:(响应OP添加的代码)

从你的数据阅读器到一个数组(虽然我不相信你需要一个数组)我会做以下事情:

' using the variable I declared earlier
DatesToHighlight = New IEnumerable(Of Date)
If reader.HasRows Then
    Dim parsedDate As Date
    While reader.Read()
        If Date.TryParse(reader(0), parsedDate) Then
            DatesToHighlight.Add(parsedDate)
        End If
    End While
End If

Dim myArrayOfDates() As Date = DatesToHighlight.ToArray()
于 2011-08-04T09:28:13.193 回答
2

假设您有一个DataTable名为 myDates 和一个Calendar名为 myCalendar 的控件:

For i As Int = 0 To myDates.Rows.Count - 1
    myCalendar.SelectedDates.Add(CType(myDates.Row(i)(0), Date)
Next

您可以在标记中声明突出显示:

<asp:Calendar ID="myCalendar" runat="server">
    <SelectedDayStyle BackColor="red" />
</asp:Calendar>

或以编程方式:

myCalendar.SelectedDayStyle.BackColor = System.Drawing.Color.Red

更新SqlDataReader(这次是VB.NET)

If reader.HasRows Then
    While reader.Read()
        myCalendar.SelectedDates.Add(CType(reader(0), Date)
    End While
End If

基于 OP 代码的更新

代码运行时是否出现任何错误? 如果正在读取的列不是 DateTime 列,SqlDataReader.GetDateTime将抛出一个。InvalidCastException

我想知道是不是格式问题?您能否验证数据库中列的数据类型,以及存储日期的格式?

我已经通过一些建议对您的代码进行了一些修改。

Imports System.Data.SqlClient

Partial Class _Default Inherits System.Web.UI.Page  

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("CleanOneConnectionString").ConnectionString

        ' Using blocks will automatically dispose of the object, and are 
        ' pretty standard for database connections
        Using connection As New SqlConnection(connectionString)
            connection.Open()     
            Dim sql As String = "Select schedule From OrderDetails Where schedule is not null"
            Dim command As SqlCommand = New SqlCommand(sql, connection)     
            Dim reader As SqlDataReader = command.ExecuteReader() 

            ' This is not needed - in fact, this line will "throw away"
            ' the first row in the row collection
            'If (reader.Read()) Then

            If (reader.HasRows) Then             
                While reader.Read()
                    myCalendar.SelectedDates.Add(CType(reader.GetDateTime(0), Date))              End While         
            End If

            reader.Close()     

            ' Not needed because of the Using block
            'connection.Close()     
        End Using

        myCalendar.SelectedDayStyle.BackColor = System.Drawing.Color.Red 
    End Sub     
End Class 
于 2011-08-04T09:29:03.193 回答