0

I'm working on datagridview in c# windows forms application and I'm loading the data from the database,Now i want to edit the cell value and save the value to the database, Here i have date and time in my database.

enter image description here

when i click the row header in datagridview the data are transfer to the text boxes and also in datetime picker

enter image description here

data are copy in textbox correctly but it gives error message when copy in datetimepicker.

code i done.

private void dgvpbx_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            txtname.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            cmbidentifier.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtplace.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            txtcity.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
            dtpdate.Value = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString());
            dtptime.Value = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString());
        }

Error message is

enter image description here

dhana
  • 83
  • 1
  • 9

2 Answers2

2

If you know the exact format in which the date and time will be in the grid, parse them accordingly:

dtpdate.Value = DateTime.ParseExact(dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString(),
    "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
dtptime.Value = DateTime.ParseExact(dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString(),
    "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

From the screenshot, your two DateTimePicker controls do seem to both give a full date, though. You might want to change those so one only picks time and the other only picks a date.

Do make sure that to put your dates and times into the grid, you convert the DateTime objects to string using exactly the same format:

(where i is the index you're currently editing)

dataGridView1.Rows[i].Cells[5].Value = dtpdate.Value.ToString("dd-MM-yyyy");
dataGridView1.Rows[i].Cells[6].Value = dtptime.Value.ToString("HH:mm:ss");
Nyerguds
  • 5,360
  • 1
  • 31
  • 63
  • Please help me sir i got another error Additional information: String was not recognized as a valid DateTime. on same line . – dhana May 20 '16 at 13:11
  • As I already said in the comments above, put the result of the `.Value.ToString()` operation in a string variable and check what it contains. – Nyerguds May 20 '16 at 13:16
0
dtpdate.Value = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString("dd-MM-yyyy"));
dtptime.Value = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString("dd-MM-yyyy"));
Alper Şaldırak
  • 1,034
  • 8
  • 10
  • That won't work... the values in the grid are _strings_, not `DateTime`; that's the whole point here. You can't do string.ToString("dd-MM-yyyy"). Otherwise you wouldn't _need_ any converting. – Nyerguds May 20 '16 at 13:06
  • what is <> in debug mode with breakpoint – Alper Şaldırak May 20 '16 at 13:07
  • Given the fact _all_ objects put into gridviews and similar controls just revert to their default .ToString() notation, the two can't be actual DateTimes, or they would look the same and give full date and time. So they're clearly just strings. – Nyerguds May 20 '16 at 13:12
  • 1
    http://stackoverflow.com/questions/31976306/how-to-show-the-complete-datetime-value-in-a-datagridview-cell-when-it-is-exactl – Alper Şaldırak May 20 '16 at 13:15
  • 1
    A possible solution is `dtpdate.Value = ((DateTime)dataGridView1.Rows[e.RowIndex].Cells[5]);` – raidensan May 20 '16 at 13:16
  • @AlperŞaldırak Oh, it has formatters? Interesting. Still, if the objects _are_ `DateTime`, the whole converting mess is completely unneeded. – Nyerguds May 20 '16 at 13:17
  • @raidensan Surely `DateTimePicker` expects a `DateTime` as value, not a string? – Nyerguds May 20 '16 at 13:19
  • @raidensan Right. I didn't consider the value inside the grid could already _be_ a `DateTime`. I was unaware the control had formatters for things like that :-\ – Nyerguds May 20 '16 at 13:21
  • @Nyerguds it is not `DateTime`, it is an `Object`. So, if you know the type of cell you can employ Casting. – raidensan May 20 '16 at 13:30
  • 1
    @raidensan I did mean the actual object inside the variable, yes. I know the `.Value` itself is `Object`. – Nyerguds May 20 '16 at 13:35