zoukankan      html  css  js  c++  java
  • [原创]创建指定RowState属性的DataRow实例

     1private void button1_Click(object sender, RoutedEventArgs e)
     2        {
     3            MessageBox.Show(CreateStateRow<cinlapdemoDataSet.CustomersDataTable,cinlapdemoDataSet.CustomersRow>().RowState.ToString());
     4        }

     5
     6        public static R CreateStateRow<T, R>(params NewRowState[] newRowState)
     7            where T : DataTable
     8            where R : DataRow
     9        {
    10            NewRowState nrs = NewRowState.Modified;
    11
    12            if (newRowState.Length > 1)
    13            {
    14                throw new Exception("创建行状态个数大于1");
    15            }

    16            else if (newRowState.Length == 1)
    17            {
    18                nrs = newRowState[0];
    19            }

    20
    21            int oldRecord = 1;
    22            int newRecord = 2;
    23
    24            if (nrs != NewRowState.Modified)
    25            {
    26                if (nrs == NewRowState.Added)
    27                {
    28                    oldRecord = -1;
    29                    newRecord = 1;
    30                }

    31                else
    32                {
    33                    oldRecord = 1;
    34                    newRecord = -1;
    35                }

    36            }

    37
    38            Type tableType = typeof(T);
    39            Type rowType = typeof(R);
    40
    41            T dataTable = (T)tableType.Assembly.CreateInstance(tableType.FullName);
    42
    43            DataRow row = dataTable.NewRow();
    44
    45            FieldInfo fiOldRecord = rowType.GetField("oldRecord", BindingFlags.NonPublic | BindingFlags.Instance);
    46            FieldInfo fiNewRecord = rowType.GetField("newRecord", BindingFlags.NonPublic | BindingFlags.Instance);
    47
    48            fiOldRecord.SetValue(row, oldRecord);
    49            fiNewRecord.SetValue(row, newRecord);
    50
    51            return (R)row;
    52        }

    53
    54        public enum NewRowState : int
    55        {
    56            Added = 0,
    57            Modified = 1,
    58            Deleted = 2
    59        }
  • 相关阅读:
    Express入门
    nodejs入门
    css实现点点点效果
    定时器详解和应用、js加载阻塞、css加载阻塞
    栈内存和堆内存有什么区别?
    webpack入门
    Ubuntu常用命令集合
    HTTP缓存机制
    125. 验证回文字符串
    算法的时间复杂度和空间复杂度(js版)
  • 原文地址:https://www.cnblogs.com/think8848/p/1164490.html
Copyright © 2011-2022 走看看