How to concatenate data from a Datagridview and Save to a single database field

Asked

Viewed 307 times

0

I want to concatenate and save the fields of a datagridview, but I don’t know how to do it.

What I Have:

method:

public void Gravar()
{
    string strQuery;
    strQuery = "INSERT INTO Prato";
    strQuery += (" VALUES(");
    strQuery += ("seq_prato.NEXTVAL,");
    strQuery += ("'" + _nome + "',");
    strQuery += ("'" + _porcao + "',");
    strQuery += ("'" + _preco + "',");
    strQuery += ("'" + _descricao + "',");
    strQuery += ("'1'");
    strQuery += (")");
    clnBancoDados ObjClnBancoDados = new clnBancoDados();
    ObjClnBancoDados.ExecutaComando(strQuery);
}

Save button:

private void btnsalvar_Click(object sender, EventArgs e) 
{ 
    if ((txtnome.Text == "") || (txtpreco.Text == "") || (txtingrediente.Text == "")) 
    { 
        MessageBox.Show("Os Campos com * são de Preenchimento Obrigatórios!"); txtnome.Focus(); 
    }
    else
    {
        clnPrato Prato = new clnPrato();
        if (txtcod.Text != "")
        {
            Prato.cod = Convert.ToInt32(txtcod.Text);
            Prato.preco = Convert.ToInt32(txtpreco.Text);  
        }

        Prato.nome = txtnome.Text;
        Prato.porcao = comboporcao.Text;
        Prato.descricao = txtdescricao.Text;

        if (ObjOperacao == clnFuncoesGerais.Operacao.Inclusao)
        {
            Prato.Gravar();
            MessageBox.Show("Dados Gravados com Sucesso!", "Novo Produto " + txtnome.Text,
        MessageBoxButtons.OK, MessageBoxIcon.Information);
        } 
    } 
}

Datagridview:

public void formataGridView()
{
    dgvprato.ColumnCount = 2;
    dgvprato.Columns[0].Name = "CÓDIGO";
    dgvprato.Columns[1].Name = "INGREDIENTES";         
}

inserir a descrição da imagem aqui

I want to concatenate the CODE column of all lines and save in _.

1 answer

3


I don’t quite understand it, but I think you’re trying to do it this way:

Replace that: strQuery += ("'1'");

therefore: strQuery += (_código.ToString());

Browser other questions tagged

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