quinta-feira, 23 de julho de 2015

CheckObject

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Web.UI;

namespace Lapuinka.Utils
{
    public static class CheckObject
    {
        public static void GetChecked(List lista, GridView gridView, string idProperty, Page page)
        {
            for (var i = 0; i < gridView.Rows.Count; i++)
            {
                var id = (gridView.DataKeys[i][idProperty]).ToString();

                for (var j = 0; j < lista.Count; j++)
                {
                    PropertyInfo propertyInfo = lista[j].GetType().GetProperty(idProperty);

                    if (propertyInfo.GetValue(lista[j], null).ToString() != id) continue;

                    var ckRelatorio = gridView.Rows[i].FindControl("ckRelatorio") as CheckBox;

                    PropertyInfo propertyInfoCheck = lista[j].GetType().GetProperty("CheckRelatorio");
                    ckRelatorio.Checked = (bool)propertyInfoCheck.GetValue(lista[j], null);
                    propertyInfoCheck.SetValue(lista[j], ckRelatorio.Checked, null);
                    break;
                }
            }

            page.Session["List"] = lista;
        }

        public static void SetChecked(List lista, GridView gridView, string idProperty)
        {
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                string id = (gridView.DataKeys[i][idProperty]).ToString();

                for (int j = 0; j < lista.Count; j++)
                {
                    PropertyInfo propertyInfo = lista[j].GetType().GetProperty(idProperty);

                    if (propertyInfo.GetValue(lista[j], null).ToString() == id)
                    {
                        var ckRelatorio = gridView.Rows[i].FindControl("ckRelatorio") as CheckBox;

                        PropertyInfo propertyInfoCheck = lista[j].GetType().GetProperty("CheckRelatorio");
                        ckRelatorio.Checked = (bool)propertyInfoCheck.GetValue(lista[j], null);
                        break;
                    }
                }
            }

            return;
        }

    }
}


Postar um comentário