Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将DateTimePicker设置为Null_C#_Datetimepicker - Fatal编程技术网

C# 将DateTimePicker设置为Null

C# 将DateTimePicker设置为Null,c#,datetimepicker,C#,Datetimepicker,我无法将Datetimepicker设置为Null,如何才能做到这一点。在我的项目中,我的要求是验证DTPif是否为空,因此我需要设置为空,我使用的代码是: { dateInsert.Format = DateTimePickerFormat.Custom; dateInsert.Text = string.Empty; } 当DateTimePicker值更改时,获取一个

我无法将Datetimepicker设置为Null,如何才能做到这一点。在我的项目中,我的要求是验证DTPif是否为空,因此我需要设置为空,我使用的代码是:

              {
                dateInsert.Format = DateTimePickerFormat.Custom;
                dateInsert.Text = string.Empty;
               }

当DateTimePicker值更改时,获取一个变量并设置其值

e、 g


使用以下代码:

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";

我有一个类似的问题,但我找不到我喜欢的答案。然而,我确实想出了一个合适的解决办法。将datetimepicker的“ShowCheckBox”属性设置为true。这将在框中的日期旁边放置一个复选框。然后添加以下代码

if (dateTimePicker1.Checked == false) 
    myVariable = ""; 
else 
    myVariable = dateTimePicker1;
就像上面提到的“karthik reddy”一样,您需要使用“CustomFormat”,但是要将null写回SQL数据库(例如),您的代码必须在“添加”或“更新”记录时检查DateTimePicker,因此您至少需要两段代码。其中a)'dtp_X'是日期时间选择器控件,b)'u NewRecord'是发送回SQL server的自定义对象,c)'TheDateProperty'是自定义对象的特定日期字段或属性

//Call this when the form Loads 
private void AllowTheDatePickersToBeSetToNothing()      
{
//This let's you set the DatePicker to nothing
dtp_X.CustomFormat = " ";
dtp_X.Format = DateTimePickerFormat.Custom;

}   

// Call this when Uploading or Adding the record (_NewRecord) to an SQL database    
private void Set_The_Field_Value_based_on_the_CustomFormat_of_the_DateTimePickers()
{
if (dtp_X.CustomFormat == " ")
    {
    //the date should be null
    _NewRecord.TheDateProperty = SqlDateTime.Null;
    }
else
    {
    _NewRecord.TheDateProperty = SqlDateTime.Parse(Convert.ToString(dtp_X.Value));
    }

}


//This button resets the Custom Format, so that the user has a way to reset the DateTimePicker Control
private void btn_Reset_dtp_X_Click(object sender, EventArgs e)
{
dtp_X.Format = DateTimePickerFormat.Custom;
dtp_X.CustomFormat = " ";
}
//Call this when the form Loads 
private void AllowTheDatePickersToBeSetToNothing()      
{
//This let's you set the DatePicker to nothing
dtp_X.CustomFormat = " ";
dtp_X.Format = DateTimePickerFormat.Custom;

}   

// Call this when Uploading or Adding the record (_NewRecord) to an SQL database    
private void Set_The_Field_Value_based_on_the_CustomFormat_of_the_DateTimePickers()
{
if (dtp_X.CustomFormat == " ")
    {
    //the date should be null
    _NewRecord.TheDateProperty = SqlDateTime.Null;
    }
else
    {
    _NewRecord.TheDateProperty = SqlDateTime.Parse(Convert.ToString(dtp_X.Value));
    }

}


//This button resets the Custom Format, so that the user has a way to reset the DateTimePicker Control
private void btn_Reset_dtp_X_Click(object sender, EventArgs e)
{
dtp_X.Format = DateTimePickerFormat.Custom;
dtp_X.CustomFormat = " ";
}