c# 常用扩展方法

DataTable

    public static class DataTableExtension
    {
        public static T GetData<T>(this DataTable dt, int rowIndex, string colName)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return default(T);
            }
            //判断是否超过行数
            if (dt.Rows.Count <= (rowIndex + 1))
            {
                return default(T);
            }

            var row = dt.Rows[rowIndex];

            var cell = row[colName];
            if (cell == null)
            {
                return default(T);
            }

            try
            {
                var data = (T)cell;
                return data;
            }
            catch
            {
                return default (T);
            }
        }
    }
posted @ 2024-03-29 08:57  暖暖De幸福  阅读(3)  评论(0编辑  收藏  举报