how to get date from data grid view into datetime picker by clicking on cell ??

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

                textId.Text = row.Cells["Appointment ID"].Value.ToString();
                textMobileNumber.Text = row.Cells["Mobile Number"].Value.ToString();
  // Problem in this line --> dtpAppointmentdate.Text = row.Cells["Appointment Date"].Value.ToString();
                comboAppointmentTime.SelectedText = row.Cells["Appointment Time"].Value.ToString();
                comboAppointmentTime.Text = row.Cells["Appointment Time"].Value.ToString();

            }
        }

Recommended Answers

All 20 Replies

Try setting the value of the DateTimePicker by converting the cell value to a Date value

dtpAppointmentdate.Value = Convert.ToDateTime(row.Cells["Appointment Date"].Value)

not working !! getting error : String was not recognized as a valid DateTime.

Put a break point on that line and check that the actual value being used follows the right format. If the data is unreliable and needs to be validated first, the DateTime.TryParse method will allow you handle that.

Actually if you are trying to get the DateTime from a string, you should be using the something like DateTime.Parse, DateTime.TryParse, DateTime.ParseExact, or DateTime.TryParseExact

If I remember right, the Convert.ToDateTime, never worked for converting a string to a DateTime. However, I do know for a fact the above ones work. The ParseExact is nice because you can say "hey this input is in this DateTime format", however, the Parse is rather smart and figuring out the pattern on it's own (I've used this for parsing industry regulated barcode, who can have mutlipe DateTime formats)

(By the way, the difference between, Parse and TryParse, is the Parse will thrown an exception if fails, while TryParse returns a true or false, and uses an 'out' for the converted value, much like the other TryParse items)

Still getting this error String was not recognized as a valid DateTime.

String was not recognized as a valid DateTime.

Could it be you are given ParseExact the format "dd/MM/yyyy", while your customformat is "dd-MM-yyyy" ?

Yeah if you look at your debugging values, you are saying the pattern is "dd-MM-yyyy" but it clear is showing a pattern of dd-MM-yyyy". You probably don't have to use DateTime.ParseExact, I wouldn't be surprised if Parse pulled it fine.

That being said, I would also strongly advise you replace that null with an Invariant Culture value. It's better practice to account of culture differences earlier on, then later and not having a good habit of it.

still not working and i also use parse but still same error i think is because time is also in datetimepicker at debugging value .
how to remove that time from date time picker ?

How does the string row.Cells["appointment date"].Value.ToString() looks like?

inserting appointment date from datetimepicker into database and in database appointment date is have only date not time

Not what I meant.
Assign string row.Cells["appointment date"].Value.ToString() to a new string variable in your code.
See what it looks like in the debugger.

30-01-2016 date looks like this appointment date and now i also set format but and still getting same error and at debugger dtpAppointmentdate.value change into dtpAppointmentdate.Value {1/29/2016 3:31:43 PM}
i also use dtoAppointment.Text = row.cells["appoinment date"].value.tostring() still getting another error .

When I use this command:

DateTimePicker1.Value = Convert.ToDateTime("30-01-2016")

It converts perfectly.

agree but in my code DateTimePicker1.value is look like 30/01/2016 with time ..
i format it but still look like this
take a look at those image

What does row.Cells["appointment date"].Value stnds for?
In your debugger I already see it's a string, so an extra ToString() is not needed.

I would suggest using the custom formatting capability of the DateTimePicker itself. It's accessed through 2 properties. Set the Format property to Custom then set the CustomFormat property to the format you want.

Did our answers where sufficient to solve your problem?
If so, mark this thread as solved.
You may help others members of this community by doing so.

no still getting error . im sreaching solution too.

@tinstaafl already done but still error.

commented: Hamza_13 30 Minutes +0

Remember that DateTimePicker is most likely pulling the DateTime format based on the cultural settings on the machine. That being said, you have to be careful about have it should expect the format.

That being said, DateTimePicker.Value, expects a value in DateTime format, this is the best move because DateTime types don't care about the culture info once they are set, as they have all those value broken up into properties (like Month, Date, Year, ext). You're goal should be getting the value from the database, and converting it to a DateTime type.

Did you say you tried this?

DateTime AppointmentTime = DateTime.Parse(row.Cells["Appointment Time"].Value.ToString());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.