CellClick event, does not return DateTime value

0

I am writing some data in DataGridView and Event CellClick should fill DateTimePicker with the value already registered, but I am doing something wrong.

Buttons: save, edit, delete, and cellclick event

private void btnGravar_Click_1(object sender, EventArgs e)

        {
            i++;
            dgvOS.Rows.Add(cbClientes.Text, dtpAgora.Value, dtpInicio.Value, dtpTermino.Value, txttotal.Text, cbPagamento.Text, cbStatus.Text, rtbDescricaoServico.Text);

            cbClientes.SelectedIndex = -1;
            dtpInicio.Text = "";
            dtpTermino.Text = "";
            cbStatus.SelectedIndex = -1;
            cbPagamento.SelectedIndex = -1;
            txttotal.Text = "0.00";
            rtbDescricaoServico.Text = "";

            btnEditar.Enabled = true;
            btnExcluir.Enabled = true;
            btnGravar.Enabled = false;

        }

        private void btnExcluir_Click(object sender, EventArgs e)
        {
            dgvOS.Rows.RemoveAt(poc);
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            dgvOS[1, poc].Value = cbClientes.Text;
            dgvOS[2, poc].Value = dtpAgora.Value;
            dgvOS[3, poc].Value = dtpInicio.Value;
            dgvOS[4, poc].Value = dtpTermino.Value;
            dgvOS[5, poc].Value = txttotal.Text;
            dgvOS[6, poc].Value = cbPagamento.Text;
            dgvOS[7, poc].Value = cbStatus.Text;
            dgvOS[8, poc].Value = rtbDescricaoServico.Text;

            MessageBox.Show("Ordem de Serviço número: " + i + " Alterado!");
        }

        private void dgvOS_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           poc = dgvOS.CurrentRow.Index; // Atualizado 
            cbClientes.Text = dgvOS[1, poc].Value.ToString();
              dtpAgora.Value = Convert.ToDateTime(dgvOS[2, poc].Value.ToString());
            dtpInicio.Value = Convert.ToDateTime(dgvOS[3, poc].Value.ToString());
            dtpTermino.Value = Convert.ToDateTime(dgvOS[4, poc].Value.ToString());
              txttotal.Text = dgvOS[5, poc].Value.ToString();
           cbPagamento.Text = dgvOS[6, poc].Value.ToString();
              cbStatus.Text = dgvOS[7, poc].Value.ToString();
   rtbDescricaoServico.Text = dgvOS[8, poc].Value.ToString();

            btnGravar.Enabled = false;
        }

Error:

System.FormatException ocorrido
  HResult=0x80131537
  Message=Cadeia de caracteres não foi reconhecida como DateTime válido.
  Source=mscorlib
  StackTrace:
   em System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   em System.Convert.ToDateTime(String value)
   em ProjetoFinal.frmOrdemServico.dgvOS_CellClick(Object sender, DataGridViewCellEventArgs e) em C:\Users\willian\source\repos\ProjetoFinal\ProjetoFinal\frmOrdemServico.cs:linha 174
   em System.Windows.Forms.DataGridView.OnCellClick(DataGridViewCellEventArgs e)
   em System.Windows.Forms.DataGridView.OnMouseClick(MouseEventArgs e)
   em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   em System.Windows.Forms.Control.WndProc(Message& m)
   em System.Windows.Forms.DataGridView.WndProc(Message& m)
   em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   em System.Windows.Forms.Application.Run(Form mainForm)
   em ProjetoFinal.Program.Main() em C:\Users\willian\source\repos\ProjetoFinal\ProjetoFinal\Program.cs:linha 19

Does anyone suggest any changes?

UPDATE

Return:

Programlayout:

    
asked by anonymous 30.10.2017 / 18:37

2 answers

0

I was able to solve the problem:

  • I was in two places with the problem: 1º I was not picking the selected line from DataGridView to solve this code in the CellClick event: poc = dgvOS.CurrentRow.Index;

  • And the other problem was the columns, which were starting with 1 being started by 0, to solve it I changed all and solved:

-

private void btnGravar_Click_1(object sender, EventArgs e)

        {
            i++;
            dgvOS.Rows.Add(cbClientes.Text, dtpAgora.Value.Date, dtpInicio.Value.Date, dtpTermino.Value.Date, txttotal.Text, cbPagamento.Text, cbStatus.Text, rtbDescricaoServico.Text);

            lblId.Text = i.ToString();
            cbClientes.SelectedIndex = -1;
            dtpInicio.Text = "";
            dtpTermino.Text = "";
            cbStatus.SelectedIndex = -1;
            cbPagamento.SelectedIndex = -1;
            txttotal.Text = "0.00";
            rtbDescricaoServico.Text = "";

            btnEditar.Enabled = true;
            btnExcluir.Enabled = true;
            btnGravar.Enabled = false;

        }

        private void btnExcluir_Click(object sender, EventArgs e)
        {
            dgvOS.Rows.RemoveAt(poc);
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            dgvOS[0, poc].Value = cbClientes.Text;
            dgvOS[1, poc].Value = dtpAgora.Value.Date;
            dgvOS[2, poc].Value = dtpInicio.Value.Date;
            dgvOS[3, poc].Value = dtpTermino.Value.Date;
            dgvOS[4, poc].Value = txttotal.Text;
            dgvOS[5, poc].Value = cbPagamento.Text;
            dgvOS[6, poc].Value = cbStatus.Text;
            dgvOS[7, poc].Value = rtbDescricaoServico.Text;

            MessageBox.Show("Ordem de Serviço número: " + i + " Alterado!");
        }

        private void dgvOS_CellClick(object sender, DataGridViewCellEventArgs e)

        {
            poc = dgvOS.CurrentRow.Index;

            cbClientes.Text = dgvOS[0, poc].Value.ToString();
             dtpAgora.Value = Convert.ToDateTime(dgvOS[1, poc].Value);
            dtpInicio.Value = Convert.ToDateTime(dgvOS[2, poc].Value);
           dtpTermino.Value = Convert.ToDateTime(dgvOS[3, poc].Value);
              txttotal.Text = dgvOS[4, poc].Value.ToString();
           cbPagamento.Text = dgvOS[5, poc].Value.ToString();
              cbStatus.Text = dgvOS[6, poc].Value.ToString();
   rtbDescricaoServico.Text = dgvOS[7, poc].Value.ToString();

            btnGravar.Enabled = false;
        }
    }
    
31.10.2017 / 10:53
0

Place a breakpoint on this line, select% with%, and press "Shift + F9" to see its result. This returned string is the cause of the problem because it is not a valid DateTime as the exception points.

    
30.10.2017 / 20:11