コード例 #1
0
        public virtual void Filtrar <T>()
        {
            bool filtro = false;

            _where = "";
            foreach (Control c in this.panelFiltro.Controls)
            {
                if (!string.IsNullOrEmpty(c.Text))
                {
                    if (!(c is MaskedTextBox))
                    {
                        filtro = true;
                    }
                    else
                    {
                        if (c.Text != ((MaskedTextBox)c).Mask)
                        {
                            filtro = true;
                        }
                    }
                }
            }
            if (!filtro)
            {
                return;
            }
            Type tipo = typeof(T);
            List <PropertyInfo> campos = new List <PropertyInfo>(tipo.GetProperties());
            string query;
            bool   fand = false;
            //Control cont = panelFiltro.Controls[1];
            //string nombreCampo = cont.Name.Substring(0, cont.Name.Length - 3);
            //query = string.Format("{0}.ToLower().Contains(@0)", nombreCampo);

            //if()
            //    query = campo

            PropertyInfo campoActual = null;

            foreach (Control cont in panelFiltro.Controls)
            {
                string nombreCampo = cont.Name.Substring(0, cont.Name.Length - 3);

                /*foreach(PropertyInfo campo in campos)
                 * {
                 *  if(campo.Name == nombreCampo)
                 *      campoActual = campo;
                 * }*/
                campoActual = campos.Find(x => x.Name == nombreCampo);
                if (campoActual == null)
                {
                    continue;
                }
                if (string.IsNullOrEmpty(cont.Text))
                {
                    continue;
                }
                if (campoActual.PropertyType == typeof(string))
                {
                    var texto = Cadenas.RemplazaApostrofos(cont.Text);
                    texto = Cadenas.ReemplazarAcentos(texto);
                    texto = Cadenas.ReemplazarComodines(texto);
                    if (fand)
                    {
                        _where += string.Format(" and {0} like '{1}%'", nombreCampo, texto);
                    }
                    else
                    {
                        _where += string.Format("{0} like '{1}%'", nombreCampo, texto);
                        fand    = true;
                    }
                }
                if (cont is FlatComboBox || cont is ComboBox)
                {
                    if (fand)
                    {
                        _where += string.Format(" and {0} = {1}", nombreCampo, ((ComboBox)cont).SelectedValue);
                    }
                    else
                    {
                        _where += string.Format(" {0} = {1}", nombreCampo, ((ComboBox)cont).SelectedValue);
                        fand    = true;
                    }
                }
                if (cont is TextBox && (campoActual.PropertyType == typeof(int) || campoActual.PropertyType == typeof(short) || campoActual.PropertyType == typeof(long)))
                {
                    if (fand)
                    {
                        _where += string.Format(" and {0} = {1}", nombreCampo, ((TextBox)cont).Text);
                    }
                    else
                    {
                        _where += string.Format(" {0} = {1}", nombreCampo, ((TextBox)cont).Text);
                        fand    = true;
                    }
                }
                if (cont is MaskedTextBox && (campoActual.PropertyType == typeof(DateTime) || campoActual.PropertyType == typeof(DateTime?)))
                {
                    var    contIni = panelHeader.Controls[nombreCampo + "Ini"];
                    string fi      = ((MaskedTextBox)contIni).MaskedTextProvider.ToDisplayString();
                    string ff      = ((MaskedTextBox)cont).MaskedTextProvider.ToDisplayString();
                    if (contIni == null)
                    {
                        throw new Exception("No se encontro control de fecha inicial");
                    }
                    if (fand)
                    {
                        _where += string.Format(" and {0} between '{1}' and '{2}'", nombreCampo, fi, ff);
                    }
                    else
                    {
                        _where += string.Format("{0} between '{1}' and '{2}'", nombreCampo, fi, ff);
                    }
                }

                /*
                 * if(cont is MaskedTextBox)
                 *  ((MaskedTextBox)cont).TextMaskFormat= MaskFormat.ExcludePromptAndLiterals;
                 *
                 * if(!string.IsNullOrEmpty(cont.Text))
                 * {
                 *  if(campoActual.PropertyType == typeof(string))
                 *  {
                 *      query = string.Format("{0}.ToLower().Contains(@0)", nombreCampo);
                 *      IQueryable<T> x =l.AsQueryable<T>().Where(query, cont.Text.ToLower());
                 *      x=l.Where(x=>x.)
                 *      if (x == null || x. == 0)
                 *          l = null;
                 *      l = x.ToList();
                 *  }
                 * }*/
            }
            if (!string.IsNullOrEmpty(_where))
            {
                CargarGrid();
                Estado = Estados.Filtrado;
            }
            return;
        }