Pull Characters from a column in the datagridviwer

Asked

Viewed 23 times

0

I have a little problem, I have a datagridviwer that I fill a table in the database, just before to fill precious take the strokes of the content of the columns. You can use Replace in a datagridviewr column, or some other way to clear the characters and then save to the database.

Follows 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();
}
  • Has can be used replace same, what is the difficulty?

  • So to tell you the truth I don’t know how to use replace in this code..

  • o . Value is an Object if I am not mistaken, it would be which field?

  • Then the value and value of the field inside datagridwier cell 4

1 answer

0


Basically:

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

note that the .Value is the type object and to function needs to be converted to text (string) to use the method Replace.

  • Virgilio thanks for the help was just what I needed, now takes away a doubt, I’m still new in the programming c#, and I wonder what server the $ at the beginning of the code?

  • @Juniorguerreiro is well explained https://answall.com/questions/128043/usando-interpola%C3%A7%C3%A3o-de-string-c-6/128284 or https://answall.com/questions/93207/string-interpolation-tem-uma-performance-bestque-string-format

  • 1

    Thank you.

Browser other questions tagged

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