I am unable to set the set the Datetimepicker to Null, how can do it. In my project my requirement is to validate the DTPif it is a null, for that I need to set to Null, The code I am using is:
{
dateInsert.Format = DateTimePickerFormat.Custom;
dateInsert.Text = string.Empty;
}
Use the following code:
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
Take a variable and set its value when DateTimePicker value changes
e.g.
DateTime? myselectedDate = null;
private void DateTimePicker1_ValueChanged(Object sender, EventArgs e) {
myselectedDate = DateTimePicker1.Value;
}
I had a similar question but I could not find an answer I liked. However, I did come up with a suitable solution. Set the "ShowCheckBox" property for the datetimepicker to true. This will put a check box next to the date in the box. Then add the following code.
if (dateTimePicker1.Checked == false)
myVariable = "";
else
myVariable = dateTimePicker1;
Like "karthik reddy" noted above you need to use the 'CustomFormat', however to write back a null to the SQL database (for example) your code has to inspect the DateTimePicker when it 'Adds' or 'Updates' a record, so you need at least two pieces of code. Where a) 'dtp_X' is the DateTimePicker control, b) '_NewRecord' is the custom object that is being sent back to the SQL server, c) 'TheDateProperty' is the particular date field or property of the Custom object
//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 = " ";
}
Related
I am having an issue with the dateTimePicker class. I need to have the DateTimePicker empty on construct, which I have achieved via this:
dateTimePicker1.MaxDate = DateTime.Now; // Ensures no future dates are set.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
My issue is that now the user has to actually click on the on the calendar to enter a date, rather than using the up down keys, or type. Is there anyway I can initialize dateTimePicker1 to " " as I have in my constructor, but also allow the user to 'Activate' it by using he arrow keys opposed to clicking on the calendar?
My ValueChanged changed event
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateTimePicker1.CustomFormat = "dd/MM/yyyy hh:mm:ss";
if (dateTimePicker1.Checked == true)
{
dateTimePicker1.Enabled = true;
dateTimePicker1.Format = DateTimePickerFormat.Short;
}
else if (dateTimePicker1.Checked == false)
{
dateTimePicker1.Enabled = false;
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
}
}
I know that DateTimePicker is not easily made nullable and in order so you have to right an extension of the class but this was a quick fix. Any ideas would be much appreciated.
I have a datetimepicker values in datagridview. (Arrival date and departure date)
Here is screen shoot.
Also, I have an edit form for the values in datagrid view.
How can I pass the values of datetimepicker from datagridview to the second form as datetimepicker value.
Here is my try:
private void arrivaldgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
edit edt = new edit();
edt.label12.Text = this.label2.Text;
edt.label13.Text = this.arrivaldgv.CurrentRow.Cells[0].Value.ToString();
edt.textBox1.Text = this.arrivaldgv.CurrentRow.Cells[1].Value.ToString();
edt.textBox2.Text = this.arrivaldgv.CurrentRow.Cells[2].Value.ToString();
edt.textBox3.Text = this.arrivaldgv.CurrentRow.Cells[3].Value.ToString();
/* edt.dateTimePicker1.Value = this.arrivaldgv.CurrentRow.Cells[4].Value; problem here */
edt.ShowDialog();
}
Try the System.Convert.ToDateTime method:
edt.dateTimePicker1.Value = System.Convert.ToDateTime(this.arrivaldgv.CurrentRow.Cells[4].Value);
You can use the DateTime.Parse method:
edt.dateTimePicker1.Value = DateTime.Parse(this.arrivaldgv.CurrentRow.Cells[4].Value);
Most of the .ToX methods use some instance methods internally, such is the case here too, which means that this should work faster than Convert.ToDateTime()
datetimepicker.Value or datetimepicker.Text is always returning the current DateTime, not the selected one. Is there any property that I need to set to get the selected date time.
Below is my code:
private void button1_Click(object sender, EventArgs e)
{
c1.cName = textBox1.Text;
c1.dlv_Time = dateTimePicker1.Value;
string temp = dateTimePicker1.Text;
textBox1.Text = "";
dateTimePicker1.Text = "";
}
In the Form UI, I am changing the time, but in the code Im always receiving the present time.
Thanks in advance
dateTimePicker1.Value.TimeOfDay.ToString()
It will return you the selected time of the date picker control in the string format.
I was editing a different timepicker and reading from a different timepicker.
I have resolved it by changing datetimepicker1 to datetimepicker2.
So I have an event that is when you click a button I want to send an int, string, datetime and bool to my database.
Right now it will get the input from my webform for the "Company" and the "DayNumber". I want to get the value of my CheckBox01 as the boolean for WeatherRain and the input from the Date. You should be able to choose the date not just take it from "now".
I don't know what would be the best way to get this done, I do have around 100 different fields to get user input from.
protected void btnTryck_Click(object sender, EventArgs e)
{
Dagbok bok = DagbokFactory.CreateNew();
bok.Company = String.Format(TextBox1.Text);
bok.DayNumber = Convert.ToInt32(TextBox19.Text);
bok.WeatherRain = false;
bok.Date = DateTime.Now;
}
In order to get the boolean from your CheckBox01 use its Checked property.
bok.WeatherRain = CheckBox01.Checked;
As for the date problem, I'd suggest using a DateTimePicker control to let the user select the date and get its value with the Value property.
Use Checkbox's checked property for this as shown below:
protected void btnTryck_Click(object sender, EventArgs e)
{
Dagbok bok = DagbokFactory.CreateNew();
bok.Company = String.Format(TextBox1.Text);
bok.DayNumber = Convert.ToInt32(TextBox19.Text);
bok.WeatherRain = CheckBox01.Checked;
bok.Date = DateTime.Now;
}
I'm setting some default value for my text fields and those fields are in read only mode. When I pass new values to the those fields and submits the form it saved default value not the new one. I have used PostBack function but it's not working. Here is my code,
protected void Page_Load(object sender, EventArgs e)
{
if (! this.IsPostBack)
{
MakeReadControl();
}
}
private void MakeReadControl()
{
((TextBox)this.FindControl("txtMedicalCheckup")).Text = System.DateTime.Now.ToShortDateString();
((TextBox)this.FindControl("txtBosiet")).Text = System.DateTime.Now.ToShortDateString();
((TextBox)this.FindControl("txtLandSurvival")).Text = System.DateTime.Now.ToShortDateString();
((TextBox)this.FindControl("txtDefensiveDriving")).Text = System.DateTime.Now.ToShortDateString();
((DropDownList)this.FindControl("comboDrivingLicense")).SelectedIndex = 1;
((TextBox)this.FindControl("txtOtherLicense")).Text = "Not available";
}
However, the drop down list updates the value correctly.
Update:-
Instead of Read Only attribute if I use enable attribute then it works fine.