1
Inside a Datagridview I have a text field and I’ve tried everything but I can’t validate it. Example:
1 - Delete columns with product name and price.
2 - There are these Datagridtextcolumn that the user type the amount of products.
3 - By clicking a Button on the grid it makes the sale of the product.
But how to prevent the user from entering anything in this field? If the user leaves the field blank it obviously gives error. I tried several ways, but in all the examples it returns me an error and the system to.
Follow the code used when
private void dv_consulta_produtos_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
linha_atual = int.Parse(e.RowIndex.ToString());
idprod = dv_consulta_produtos["idprodData", linha_atual].Value.ToString();
nome_produto = dv_consulta_produtos["nomeData", linha_atual].Value.ToString();
cod_produto = dv_consulta_produtos["codprodutoData",linha_atual].Value.ToString();
modelo = dv_consulta_produtos["modeloData", linha_atual].Value.ToString();
marca = dv_consulta_produtos["marcaData", linha_atual].Value.ToString();
ncm = dv_consulta_produtos["ncmData", linha_atual].Value.ToString();
qtd = dv_consulta_produtos["qtd1", linha_atual].Value.ToString();
**//AQUI o Botao vender é acionado**
if (dv_consulta_produtos.Columns[e.ColumnIndex].Name == "bt_vender")
{
**// AQUI tentei de tudo mas não consigo fazer funcionar o tratamento de valor nulo**
if (qtdDataGridViewTextBoxColumn == null)
{
MessageBox.Show("Quantidade não pode ser nulo");
return;
}
else
{
MySqlConnection conn2 = conexao.obterConexao();
MySqlCommand comando_cadastro = new MySqlCommand("INSERT INTO vendas_itens (N_OS, IDPROD, COD_PRODUTO, NOME, MODELO, MARCA, NCM, QTD)" +
"VALUES('" + n_os + "','" + idprod + "', '" + cod_produto + "', '" + nome_produto + "', '" + modelo + "' , '" + marca + "', '" + ncm + "', '" + qtd + "')", conn2);
comando_cadastro.ExecuteNonQuery();
MessageBox.Show("Produto " + nome_produto + " inserido com sucesso");
conexao.fechaConexao();
}
}
}
Treatments have been tried
qtd = dv_consulta_produtos["qtd1", linha_atual].Value.ToString();
if (qtd =="" || qtd == "0")
{
MessageBox.Show("Quantidade não pode ser nulo");
return;
}
You should post your code, explain the ways you have tried and failed, missing information on your question.
– João Paulo Amorim
Hello. I edited and put the code.
– Cristiano Almeida
you have tried type if (string.Isnullorempty(qtdDataGridViewTextBoxColumn.Tostring()))
– João Paulo Amorim
which message appears when you try the negotiations? is a mistake? does not enter ifs?
– João Paulo Amorim
Hi Paulo. All right? I’m going to take this test you indicated! The error that appears is System.Nullreferenceexception: 'Undefined object reference for an instance of an object.'
– Cristiano Almeida
@João Paulo Amorim. Good evening. I tried what you told me and I still have the same defect. When I put a value inside Datagridviewtextboxcolumn like 1 for example the code works, when there is nothing it stops. and appears There was an untreated exception of type "System.Nullreferenceexception" in SYSFOCO2.exe Object reference not defined for an instance of an object.
– Cristiano Almeida
got it, in case of this error you are using this if condition (Qtd =="" || Qtd == "0") ? if yes, try this way if (Qtd == null || Qtd =="" || Qtd == "0")
– João Paulo Amorim
@Joãopauloamorim. The error remains. I believe I must be missing something in the whole structure. I will try to redo line by line and if I have any answer I put here! Thanks for the help
– Cristiano Almeida
Yes, check exactly where you are popping the bug
– João Paulo Amorim