In a unit of measure register, I have to check if the unit the user wants to register, if it already exists, then since I have a DataGrid
populated by a DataTable
, I thought of going through DataTable
and check if it already exists.
Then I made the code like this:
//botão salvar
private void simpleButton2_Click(object sender, EventArgs e)
{
//Verifico se os dois textbox estao preenchidos
if (textBox1.Text != string.Empty && textBox2.Text != string.Empty)
{
// defino minha string de filtro
string exp = "SIGLA = " + "'" + textBox2.Text + "'";
// Executo a busca com base no meu filtro
DataRow[] find_sigla = dat.Select(exp);
// se o retorno da busca for 0 faz a ação
if (find_sigla.Count() == 0)
{
//Insiro o valor no banco
Classes.Cadastro.Cadastro_estqUn cad_estqun = new Classes.Cadastro.Cadastro_estqUn();
if (cad_estqun.cadastro_unidade(textBox1.Text, textBox2.Text) > 0)
{
textBox1.Text = "";
textBox2.Text = "";
}
}
else
{
MessageBox.Show("Unidade de medida já cadastrada.", "Salvar", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
So I've heard that this method has lost performance, right? Does anyone have a better solution?
Thank you.