public class DataColumn
{
#region "Properties"
///
/// 列名
///
public string ColumnName {
get;
set; }
///
/// 类型
///
public Type DataType {
get;
set; }
///
/// 列标题
///
public string Caption {
get;
set; }
///
/// 是否允许用户改变列的大小
///
public bool AllowResize {
get;
set; }
///
///是否允许用户进行排序
///
public bool AllowSort {
get;
set; }
///
/// 是否允许用户进行重新排序
///
public bool AllowReorder {
get;
set; }
///
/// 是否只读
///
public bool ReadOnly {
get;
set; }
#endregion ///
/// 构造并且赋初始值
///
/// 列名
public DataColumn(
string columnName)
{
this.ColumnName = columnName;
this.Caption = columnName;
this.AllowResize =
true;
this.AllowSort =
true;
this.AllowReorder =
true;
this.ReadOnly =
false;
}
///
/// 重载构造
///
/// 列名
/// 列标题
/// 是否允许改变列大小
/// 是否允许排序
/// 是否允许重新排序
/// 列只读
public DataColumn(
string columnName,
string caption,
bool allowResize,
bool allowSort,
bool allowReorder,
bool readOnly)
{
this.ColumnName = columnName;
this.Caption = caption;
this.AllowResize = allowResize;
this.AllowSort = allowSort;
this.AllowReorder = allowReorder;
this.ReadOnly = readOnly;
}
}
///
/// DataColumn集合,继承与list
///
public class DataColumnCollection : List
{
///
/// 隐藏List类中add方法,重新定义Add方法,判断有重复列的时候报出异常
///
///
public new void Add(DataColumn dc)
{
foreach (DataColumn curColumn in this)
{
if (dc.ColumnName == curColumn.ColumnName)
{
throw new Exception(String.Format("该列已经存在", dc.ColumnName));
}
}
base.Add(dc);
}
}
列都已经准备好了,接下来是伟大的行了,早在很久之前DataRow曾经一度风靡,我突然想到了一些很矜持的老程序员。。想到了久违的Ado...
public class DataRow
{
public Dictionary<
string,
object> items {
set;
get; }
public DataRow()
{
this.items =
new Dictionary<
string,
object>();
}
///
/// DataRow类索引器 (DataRow[.....])
///
///
///
public object this[
string key]
{
set { items[key] = value; }
get {
return items[key]; }
}
///
/// 通过emit反射在内存中创建出一个包含属性的类
///
///
public Assembly EmitAssembly()
{
AssemblyName assemblyName =
new AssemblyName(
"DataRowAssembly");
AssemblyBuilder assemblyBuilder=Thread.GetDomain().DefineDynamicAssembly(assemblyName,AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(
"DataRowModel",
true);
TypeBuilder typeBuilder = moduleBuilder.DefineType(
"DataRowObject",TypeAttributes.Public|TypeAttributes.Class);
foreach(KeyValuePair<
string,
object> pair
in items)
{
BuilderFieldsAndProperty(typeBuilder, pair.Key,pair.Value.GetType());
}
typeBuilder.CreateType();
return assemblyBuilder;
}
///
/// 通过emit反射创建字段和属性
///
/// TypeBuilder
/// 需要创建的属性名
/// 包含该属性的类的类型
public void BuilderFieldsAndProperty(TypeBuilder myTypeBuilder,
string name, Type type)
{
FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(name, type, FieldAttributes.Private);
PropertyBuilder myPropertyBuilder = myTypeBuilder.DefineProperty(name.ToUpper(), PropertyAttributes.HasDefault, type,
null);
MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
MethodBuilder getMethodBuilder = myTypeBuilder.DefineMethod(
"get_" + name, getSetAttr, type, Type.EmptyTypes);
ILGenerator custNameGetIL = getMethodBuilder.GetILGenerator();
custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld, myFieldBuilder);
custNameGetIL.Emit(OpCodes.Ret);
MethodBuilder setMethodBuilder = myTypeBuilder.DefineMethod(
"set_" + name, getSetAttr,
null,
new Type[] { type });
ILGenerator custNameSetIL = setMethodBuilder.GetILGenerator();
custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, myFieldBuilder);
custNameSetIL.Emit(OpCodes.Ret);
myPropertyBuilder.SetGetMethod(getMethodBuilder);
myPropertyBuilder.SetSetMethod(setMethodBuilder);
}
}
Assembly rowAssembly = row.EmitAssembly();
object c=rowAssembly.CreateInstance(
"DataRowObject");
Type type = rowAssembly.GetType(
"DataRowObject");
public class DataTable
{
///
/// DataTable 的名字
///
public string Name {
get;
set; }
///
/// DataRow的集合
///
public DataRowCollection Rows {
get;
set; }
///
/// DataColumn的集合
///
public DataColumnCollection Columns {
get;
set; }
///
/// 构造函数并且赋初始值或创建对象
///
///
public DataTable(
string name )
{
this.Name = name;
this.Rows =
new DataRowCollection();
this.Columns =
new DataColumnCollection();
}
}
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
List<
string> firstNames =
new List<
string>() {
"a",
"b",
"c"};
List<
string> lastNames =
new List<
string>() {
"Jimmy",
"Jimmy2",
"Jimmy3"};
DataTable dt =
new DataTable(
"MyDataTable");
DataColumn dc1 =
new DataColumn(
"col1");
dc1.Caption =
"First Name";
dc1.ReadOnly =
true;
dc1.DataType =
typeof(String);
dc1.AllowResize =
true;
dc1.AllowSort =
true;
dc1.AllowReorder =
true;
dt.Columns.Add(dc1);
DataColumn dc2 =
new DataColumn(
"col2");
dc2.Caption =
"Last Name";
dc2.ReadOnly =
true;
dc2.DataType =
typeof(String);
dc2.AllowResize =
true;
dc2.AllowSort =
true;
dc2.AllowReorder =
true;
dt.Columns.Add(dc2);
Random r =
new Random();
for (
int i =
0; i <
15; i++)
{
DataRow dr =
new DataRow();
dr[
"col1"] = firstNames[r.Next(firstNames.Count)];
dr[
"col2"] = lastNames[r.Next(lastNames.Count)];
dt.Rows.Add(dr);
}
this.MyDataGrid.DataSoruce = dt;
this.MyDataGrid.DataBind();
}
}