C# GridView和通用列表

C# GridView和通用列表,c#,asp.net,linq,gridview,datatable,C#,Asp.net,Linq,Gridview,Datatable,对于我当前的项目,我们有两个表需要显示/更新/插入/删除/等等。我使用GridView,因为它对所涉及的数据最有意义。所以我使用LINQ从数据库中获取数据。到目前为止,非常简单的查询…GetAll和GetByID类型的东西。 因此,我的查询如下所示: List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.ToList(); 这是I类访问数据的方法: public static class UserClass {

对于我当前的项目,我们有两个表需要显示/更新/插入/删除/等等。我使用GridView,因为它对所涉及的数据最有意义。所以我使用LINQ从数据库中获取数据。到目前为止,非常简单的查询…GetAll和GetByID类型的东西。
因此,我的查询如下所示:

List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.ToList();
这是I类访问数据的方法:

public static class UserClass
{
    private static nsdc_supplyDataContext DataContext1 = new nsdc_supplyDataContext();

    #region SELECTS
    public static List<ds_Users_old> GetAllUsers()
    {
        List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.ToList();
        return selectedUser;
    }

    public static DataTable GetAllUsersDT()
    {
        List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.ToList();
        DataTable selectedUserDT = ConvertToDataTable(selectedUser);
        return selectedUserDT;
    }

    public static List<ds_Users_old> GetUsersByWorkEmail(string email)
    {
        List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.Where(user => user.WorkEmail == email).ToList();
        return selectedUser;
    }

    public static DataTable GetUsersByWorkEmailDT(string email)
    {
        List<ds_Users_old> selectedUsers = DataContext1.ds_Users_olds.Where(user => user.WorkEmail == email).ToList();
        DataTable selectedUsersDT = ConvertToDataTable(selectedUsers);
        return selectedUsersDT;
    }
    #endregion

    public static DataTable ConvertToDataTable<T>(IList<T> data)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;
    }
}
公共静态类UserClass
{
私有静态nsdc_supplyDataContext DataContext1=新nsdc_supplyDataContext();
#区域选择
公共静态列表GetAllUsers()
{
列出selectedUser=DataContext1.ds_Users_olds.ToList();
返回所选的用户;
}
公共静态数据表GetAllUsersDT()
{
列出selectedUser=DataContext1.ds_Users_olds.ToList();
DataTable selectedUserDT=ConvertToDataTable(selectedUser);
返回selectedUserDT;
}
公共静态列表GetUsersByWorkerMail(字符串电子邮件)
{
列出selectedUser=DataContext1.ds\u Users\u olds.Where(user=>user.WorkEmail==email.ToList();
返回所选的用户;
}
公共静态数据表GetUsersByWorkEmailDT(字符串电子邮件)
{
列出selectedUsers=DataContext1.ds_Users_olds.Where(user=>user.WorkEmail==email.ToList();
DataTable selectedUsersDT=ConvertToDataTable(selectedUsers);
返回selectedUsersDT;
}
#端区
公共静态数据表ConvertToDataTable(IList数据)
{
PropertyDescriptorCollection属性=TypeDescriptor.GetProperties(typeof(T));
DataTable=新的DataTable();
foreach(属性中的PropertyDescriptor属性)
table.Columns.Add(prop.Name,Nullable.GetUnderlyingType(prop.PropertyType)??prop.PropertyType);
foreach(数据中的T项)
{
DataRow行=table.NewRow();
foreach(属性中的PropertyDescriptor属性)
行[prop.Name]=prop.GetValue(项)??DBNull.Value;
table.Rows.Add(行);
}
返回表;
}
}

这看起来很混乱,感觉必须有一种更优雅的方式来完成。

既然您使用对象列表作为数据源,那么您需要实现自己的排序逻辑,如下所示:

public sealed class GenericComparer<T> : IComparer<T>
{
    public enum SortOrder
    {
        Ascending = 0,
        Descending = 1
    }

    private string sortColumn;
    private SortOrder sortingOrder;

    public string SortColumn
    {
        get
        {
            return this.sortColumn;
        }
    }

    public SortOrder SortingOrder
    {
        get
        {
            return this.sortingOrder;
        }
    }

    public GenericComparer(string theSortColumn, SortOrder theSortingOrder)
    {
        this.sortColumn = theSortColumn;
        this.sortingOrder = theSortingOrder;
    }

    public int Compare(T x, T y)
    {
        PropertyInfo thePropertyInfo = typeof(T).GetProperty(this.sortColumn);
        IComparable object1 = (IComparable)thePropertyInfo.GetValue(x, null);
        IComparable object2 = (IComparable)thePropertyInfo.GetValue(y, null);
        if (this.sortingOrder == SortOrder.Ascending)
        {
            return object1.CompareTo(object2);
        }
        else
        {
            return object2.CompareTo(object1);
        }
    }
}
公共密封类GenericComparer:IComparer
{
公共枚举排序器
{
升序=0,
递减=1
}
私有字符串排序列;
私人分拣员分拣订单;
公共字符串排序列
{
得到
{
返回此.sort列;
}
}
公共分拣机分拣订单
{
得到
{
返回此.sortingOrder;
}
}
公共泛型比较器(字符串排序列,排序顺序排序器排序顺序)
{
this.sort列=排序列;
this.sortingOrder=排序顺序;
}
公共整数比较(TX,TY)
{
PropertyInfo thePropertyInfo=typeof(T).GetProperty(this.sortColumn);
IComparable object1=(IComparable)thePropertyInfo.GetValue(x,null);
IComparable object2=(IComparable)thePropertyInfo.GetValue(y,null);
if(this.sortingOrder==SortOrder.Ascending)
{
返回object1.CompareTo(object2);
}
其他的
{
返回object2.CompareTo(object1);
}
}
}
现在,在对对象列表上的
.Sort()
方法的调用中,您传递了该帮助器类的一个新实例(将要排序的列表属性和排序方向传递给它-升序或降序)


因为上面的比较器逻辑使用泛型,所以您可以传递任何想要排序的类型(即
int
DateTime
,甚至域对象)。

因为您使用对象列表作为数据源,所以您需要实现自己的排序逻辑,如下所示:

public sealed class GenericComparer<T> : IComparer<T>
{
    public enum SortOrder
    {
        Ascending = 0,
        Descending = 1
    }

    private string sortColumn;
    private SortOrder sortingOrder;

    public string SortColumn
    {
        get
        {
            return this.sortColumn;
        }
    }

    public SortOrder SortingOrder
    {
        get
        {
            return this.sortingOrder;
        }
    }

    public GenericComparer(string theSortColumn, SortOrder theSortingOrder)
    {
        this.sortColumn = theSortColumn;
        this.sortingOrder = theSortingOrder;
    }

    public int Compare(T x, T y)
    {
        PropertyInfo thePropertyInfo = typeof(T).GetProperty(this.sortColumn);
        IComparable object1 = (IComparable)thePropertyInfo.GetValue(x, null);
        IComparable object2 = (IComparable)thePropertyInfo.GetValue(y, null);
        if (this.sortingOrder == SortOrder.Ascending)
        {
            return object1.CompareTo(object2);
        }
        else
        {
            return object2.CompareTo(object1);
        }
    }
}
公共密封类GenericComparer:IComparer
{
公共枚举排序器
{
升序=0,
递减=1
}
私有字符串排序列;
私人分拣员分拣订单;
公共字符串排序列
{
得到
{
返回此.sort列;
}
}
公共分拣机分拣订单
{
得到
{
返回此.sortingOrder;
}
}
公共泛型比较器(字符串排序列,排序顺序排序器排序顺序)
{
this.sort列=排序列;
this.sortingOrder=排序顺序;
}
公共整数比较(TX,TY)
{
PropertyInfo thePropertyInfo=typeof(T).GetProperty(this.sortColumn);
IComparable object1=(IComparable)thePropertyInfo.GetValue(x,null);
IComparable object2=(IComparable)thePropertyInfo.GetValue(y,null);
if(this.sortingOrder==SortOrder.Ascending)
{
返回object1.CompareTo(object2);
}
其他的
{
返回object2.CompareTo(object1);
}
}
}
现在,在对对象列表上的
.Sort()
方法的调用中,您传递了该帮助器类的一个新实例(将要排序的列表属性和排序方向传递给它-升序或降序)


因为上面的比较器逻辑使用泛型,所以您可以传递任何想要排序的类型(即
int
DateTime
,甚至域对象)。

这就成功了。对于这样一个简单的问题,这似乎仍然是一个非常庞大的解决方案,但它是迄今为止我见过的最优雅的解决方案。谢谢。@Jeremy-是的,我也希望有一个更简单的解决方案,但好消息是,如果您选择这样做,您可以在web应用程序中将其作为实用程序类重新使用。很高兴这有帮助。您也可以投票支持这个答案。:-)这就成功了。对于这样一个简单的问题,这似乎仍然是一个非常庞大的解决方案,但它是迄今为止我见过的最优雅的解决方案。谢谢。@Jeremy-是的,我希望在那里
public sealed class GenericComparer<T> : IComparer<T>
{
    public enum SortOrder
    {
        Ascending = 0,
        Descending = 1
    }

    private string sortColumn;
    private SortOrder sortingOrder;

    public string SortColumn
    {
        get
        {
            return this.sortColumn;
        }
    }

    public SortOrder SortingOrder
    {
        get
        {
            return this.sortingOrder;
        }
    }

    public GenericComparer(string theSortColumn, SortOrder theSortingOrder)
    {
        this.sortColumn = theSortColumn;
        this.sortingOrder = theSortingOrder;
    }

    public int Compare(T x, T y)
    {
        PropertyInfo thePropertyInfo = typeof(T).GetProperty(this.sortColumn);
        IComparable object1 = (IComparable)thePropertyInfo.GetValue(x, null);
        IComparable object2 = (IComparable)thePropertyInfo.GetValue(y, null);
        if (this.sortingOrder == SortOrder.Ascending)
        {
            return object1.CompareTo(object2);
        }
        else
        {
            return object2.CompareTo(object1);
        }
    }
}