在Datagridview控件中验证/禁用日历列的日期

时间:2014-09-12 17:20:24

标签: c# winforms datagridview .net-3.5

有关在datagridview控件中禁用日历列中的日期的建议吗。请求

-> User is not allowed to select a date which is less than today's date and the next 1 month
dates.

Example -
Today's Date -  **Sep 12th 2014** 
Now all the dates less than Sep12th,2014 are disabled and the dates till **Oct12,2014**
i.e till next month are disabled.
So user is allowed to select the dates only after Oct12th,2014.

--> When the calendar column is added to the Datagrid..it must already show the dates from next month.As per above example from
     

Oct13th,2014。

您能否就如何实现这一目标提出建议。

谢谢, Prathap

1 个答案:

答案 0 :(得分:0)

希望这会让你开始。我用一个名为“Date”的列设置了一个datagridview,列中列出了每行中的不同日期。要确定哪些日期只读,我使用以下代码:

Dim myDate As Date = Date.Today
Dim rowNumber As Integer = 0
Dim rowDate As Date = Nothing
Dim enabledDate As Date = myDate.AddDays(30)

Do Until rowDate = enabledDate
    rowDate = DataGridView1.Rows(rowNumber).Cells(0).Value

    If rowDate < enabledDate Then
        DataGridView1.Rows(rowNumber).Cells(0).ReadOnly = True
        DataGridView1.Rows(rowNumber).Cells(0).Style.ForeColor = Color.Gray
    Else
        DataGridView1.Rows(rowNumber).Cells(0).ReadOnly = False
        DataGridView1.Rows(rowNumber).Cells(0).Style.ForeColor = Color.Black
    End If

    rowNumber = rowNumber + 1
Loop

设置方式虽然你需要根据它的月份使用不同的值填充'enabledDate'。这个月你会增加30天,但下个月需要增加31天。

希望这有帮助。