Example #1
0
        private void btnBusqueda_Click(object sender, EventArgs e)
        {
            MessageBox.Show(txtDescBusqueda.Text);
            try
            {
                int       indice  = cmbBusqueda.SelectedIndex;
                string    columna = cmbBusqueda.Items[indice].ToString();
                DataTable tabla   = objetoCN.Buscar(txtDescBusqueda.Text, columna);

                if (tabla != null)
                {
                    // Don't use rows.Count. That's asking for how many rows exist.If there are many, it will take some time to count them.All you really want to know is "is there at least one?" You don't care if there are 10 or 1000 or a billion. You just want to know if there is at least one. If I give you a box and ask you if there are any marbles in it, will you dump the box on the table and start counting? Of course not. Using LINQ, you might think that this would work:
                    bool hasRows = tabla.Rows.GetEnumerator().MoveNext();
                    //if (tabla.Rows.Count > 0)
                    if (hasRows)
                    {
                        //do your code
                        dataGridView1.DataSource = tabla;
                        MessageBox.Show("Se busco Correctamente " + tabla.Rows.Count.ToString());
                        //MostrarProductos();
                        //limpiarForm();
                    }
                    else
                    {
                        MessageBox.Show("No hubo resultados");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("No se buscar por:" + ex);
            }
        }