dev初识 拖动分组
2015-10-04 17:55
351 查看
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>();
}
}
}
<%@ 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>();
}
}
}
相关文章推荐
- 生命游戏介绍
- 使用循环链表实现约瑟夫环
- Internet采用的网络协议以及该协议的主要层次结构
- tomcat的配置与安装
- Activiti入门教程十一(来杯流程喝喝吧)
- 线程,进程,任务
- BestCoder Round #58(Inversion-线段树)
- C 中的typedef应用
- 结对项目总结
- 四大域对象,和jsp的九大隐式对象,已经el表达式的11个隐式对象
- 20151004的NOIP模拟赛
- 【软件工程】结构化软件设计
- sicily 1006. Team Rankings
- 最好的单例模式 Java实现
- 第三周-删除线性表的元素
- NOIP四校联训Round2小结
- CentOS7部署LAMP之phpMyAdmin、wordpress、Discuz
- 表达式求值
- SQL Server解决分页问题(三种方法)
- 《测试驱动开发―31重构》