Validate the contents of a cell in the DataGridView

-2

I need some help. I am doing an insertion of items into a request, using a DataGridView, in that DataGridView I have the code column, , batch , and qtda . I need to validate the code column, so if I have already entered code 1, it does not re-enter code 1, ie I can not have repeated items in grid .

Follow the insert 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.

    
asked by anonymous 15.01.2018 / 12:59

1 answer

0

Do iterate in your DGW_itens and verify that 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: link

    
16.01.2018 / 21:27