Take Character from a column in the datagridviwer

0

I'm having a problem, I have a datagridviwer that populates a table in the database, only to fill in the precious points of the contents of the columns. You can use Replace in a datagridviewr column, or some other way to clean the characters and then write to the database.

Follow my code.

public void Inseri()
{
        conex1.Open();
        SqlCommand cadastro = new SqlCommand("usp_InseriCadastro", conex1);
        cadastro.CommandType = CommandType.StoredProcedure;
        for (int i = 0; i < dgw_cadastro.Rows.Count -1; i++)
        {
            cadastro.Parameters.Clear();
            cadastro.Parameters.AddWithValue("@NOME",dgw_cadastro.Rows[i].Cells[0].Value);
            cadastro.Parameters.AddWithValue("@SOBRENOME",dgw_cadastro.Rows[i].Cells[1].Value);
            cadastro.Parameters.AddWithValue("@DDD",Convert.ToInt32(dgw_cadastro.Rows[i].Cells[2].Value));
            cadastro.Parameters.AddWithValue("@TELEFONE",Convert.ToInt32(dgw_cadastro. Rows[i].Cells[3].Value));
            cadastro.Parameters.AddWithValue("@CPF",dgw_cadastro.Rows[i].Cells[4].Value);
            cadastro.Parameters.AddWithValue("@DATANASC",Convert.ToDateTime(dgw_cadastro.Rows[i].Cells[5].Value));
            cadastro.ExecuteNonQuery();
        }
        conex1.Close();
}
    
asked by anonymous 20.03.2018 / 17:19

1 answer

0

Basically:

string cpf = $"{dgw_cadastro.Rows[i].Cells[4].Value}".Replace(".","").Replace("-",".");
cadastro.Parameters.AddWithValue("@CPF", cpf);

Note that .Value is of type object and to work it needs to be converted to text ( string ) to use the Replace method.

    
20.03.2018 / 17:28