Validate the contents of a Datagridview cell

Asked

Viewed 140 times

-1

I need a help. I’m inserting items into an order using a Datagridview, in that Datagridview got the spine code, description, lot and qtda. I need to validate the column code, making sure that if I have already entered code 1, it does not re-enter code 1, meaning I cannot have repeated items in the grid.

Follow the insertion command, Button method Inseri_Itens

private void btn_inserir_Click(object sender, EventArgs e)
    {
        if (txt_qtda.Text != "")
        {
            DGW_itens.Rows.Add(txt_codigo.Text, txt_produto.Text, cb_lote.Text, txt_fabric.Text, txt_qtda.Text, txt_numero.Text);

            txt_codigo.Text = "";
            txt_produto.Text = "";
            cb_lote.Items.Clear();
            cb_lote.Text = "";
            txt_fabric.Text = "";
            txt_qtda.Text = "0.00";
            txt_idsolicitacao.Text = "";
            btn_gravar.Enabled = true;
        }

        else
        {
            MessageBox.Show("Não existem itens a serem incuidos, por favor verifique se o campo Qtda. esta preenchido!!!");
            return;

        }
    }

thank you in advance.

1 answer

0


Iterate your Dgw_items and check if the code to be inserted already exists on the grid.

for (int i = 0; i < DGW_itens.Rows.Count; i++)
{
    if (txt_codigo.Text == DGW_itens.Rows[i].Cells[0].Value.ToString())
    {
      //Seu tratamento para código ja existente no grid
    }
    else
     {
     if (txt_qtda.Text != "")
        {
            DGW_itens.Rows.Add(txt_codigo.Text, txt_produto.Text, cb_lote.Text, txt_fabric.Text, txt_qtda.Text, txt_numero.Text);

            txt_codigo.Text = "";
            txt_produto.Text = "";
            cb_lote.Items.Clear();
            cb_lote.Text = "";
            txt_fabric.Text = "";
            txt_qtda.Text = "0.00";
            txt_idsolicitacao.Text = "";
            btn_gravar.Enabled = true;
        }
 else
        {
            MessageBox.Show("Não existem itens a serem incuidos, por favor verifique se o campo Qtda. esta preenchido!!!");
            return;

        }
   }
}

Solution in: https://stackoverflow.com/questions/21385597/preventing-duplicates-before-insert-in-datagridview

  • Vlw Andre gave it right here thank you very much.

Browser other questions tagged

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