Cellclick event, does not return Datetime value

Asked

Viewed 64 times

0

I’m recording some data on DataGridView and in the Event CellClick should fill in the DateTimePicker with the value already registered, but I’m doing something wrong.

Buttons: record, edit, delete and event cellclick

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

Someone suggests me some change?

UPDATE

Return:

inserir a descrição da imagem aqui

Layout of the program:inserir a descrição da imagem aqui

  • Young man, what is the return of dgvOS[2, poc].Value?

  • I’ll show you, I’ll update

2 answers

0


I managed to solve the problem:

  • The problem was in two places: 1º I wasn’t taking the selected line from DataGridView to add this code to the event Cellclick: poc = dgvOS.CurrentRow.Index;

  • And the other problem was the columns, which were starting with 1 and starting with 0, to solve I changed them 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;
        }
    }

0

Put a breakpoint on that line, select dgvOS[2, poc].Value and press "Shift + F9" to see its result. This string returned is the cause of the problem as it is not a valid Datetime as pointed out by the exception.

  • So this line was in trouble anyway, I wasn’t getting her index now I added it to the code poc = dgvOS.CurrentRow.Index;, but keeps making mistakes and I changed to: dtpAgora.Value = Convert.ToDateTime(dgvOS[2, poc].Value);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.