标签:
1.前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication15.WebForm1" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False">
<SettingsPager PageSize="30" Position="TopAndBottom" SEOFriendly="Enabled" >
</SettingsPager>
<Settings ShowGroupPanel="True" ShowGroupFooter="VisibleAlways"></Settings>
<SettingsBehavior AllowFixedGroups="False" AllowGroup="True" AllowDragDrop="True"/>
<Columns>
<dx:GridViewDataTextColumn FieldName="No" UnboundType="Integer" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="Name" UnboundType="String" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<GroupSummary>
<dx:ASPxSummaryItem DisplayFormat="合计:0" FieldName="Name" SummaryType="Sum" />
</GroupSummary>
</dx:ASPxGridView>
</div>
</form>
</body>
</html>
2.后台代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication15
{
public class Student
{
public int No { get; set; }
public int Name { get; set; }
}
public static class ListAndTableExt
{
public static DataTable ToDataTable<T>(this List<T> list) where T : class, new()
{
DataColumn column = null;
DataTable table = new DataTable();
List<PropertyInfo> ps = typeof(T).GetProperties().ToList();
ps.ForEach(p =>
{
if (!p.PropertyType.IsGenericType)
{
column = new DataColumn(p.Name, p.PropertyType);
}
else
{
if (p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
column = new DataColumn(p.Name, Nullable.GetUnderlyingType(p.PropertyType));
}
}
if (column != null)
{
table.Columns.Add(column);
}
});
list.ForEach(obj =>
{
DataRow row = table.NewRow();
ps.ForEach(p =>
{
row[p.Name] = p.GetValue(obj, null);
});
table.Rows.Add(row);
});
return table;
}
public static List<T> ToList<T>(this DataTable table) where T : class, new()
{
List<PropertyInfo> ps = typeof(T).GetProperties().ToList();
List<T> list = new List<T>();
foreach (DataRow row in table.Rows)
{
T obj = new T();
foreach (DataColumn col in table.Columns)
{
ps.ForEach(p =>
{
if (p.Name == col.ColumnName)
{
if (!p.PropertyType.IsGenericType)
{
p.SetValue(obj, string.IsNullOrEmpty(row[p.Name].ToString()) ? null : Convert.ChangeType(row[p.Name].ToString(), p.PropertyType));
}
else
{
if (p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
p.SetValue(obj, string.IsNullOrEmpty(row[p.Name].ToString()) ? null : Convert.ChangeType(row[p.Name], Nullable.GetUnderlyingType(p.PropertyType)));
}
}
}
});
}
list.Add(obj);
}
return list;
}
}
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Student> stuList = new List<Student>();
for (int i = 0; i < 100; i++)
{
stuList.Add(new Student() { No = i, Name = i });
}
stuList.Add(new Student() { No = 1, Name = 22 });
stuList.Add(new Student() { No = 1, Name = 33 });
DataTable table = stuList.ToDataTable();
ASPxGridView1.DataSource = table;
ASPxGridView1.DataBind();
List<Student> students = table.ToList<Student>();
}
}
}
标签:
原文地址:http://www.cnblogs.com/kexb/p/4854778.html