/// <summary>
/// DataRow 转 Model
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="row"></param>
/// <returns></returns>
public static IList<T> DataGridViewSelectedRowsToModel<T>( DataGridView dgv)
{  IList<T> list = null;

            if (dgv != null)
            {
                list = new List<T>();
                foreach (DataGridViewRow dgvr in dgv.SelectedRows)
                {
                    
                    T obj = default(T);
                    obj = Activator.CreateInstance<T>();
                    foreach (DataGridViewColumn dgvc in dgv.Columns)
                    {
                        PropertyInfo prop = obj.GetType().GetProperty(dgvc.DataPropertyName);
                        if (prop == null) { continue; }

                        var dgvrModel = dgvr.Cells[dgvc.Name].Value;
                        object value = null;
                        if (dgvrModel != null)
                        {
                            value = dgvrModel;
                        }

                        //非泛型
                        if (!prop.PropertyType.IsGenericType)
                            value = string.IsNullOrEmpty(value + "") ? null : Convert.ChangeType(value, prop.PropertyType);
                        else//泛型
                        {
                            Type genericTypeDefinition = prop.PropertyType.GetGenericTypeDefinition();
                            if (genericTypeDefinition == typeof(Nullable<>))
                            {
                                value = string.IsNullOrEmpty(value + "") ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(prop.PropertyType));
                            }
                        }
                        if (prop.CanWrite)    //判断其是否可写
                            prop.SetValue(obj, value, null);

                    }
                    list.Add(obj);
                }
                
               
            }
            return list;
}

C# Winform 下 DataGridView 行(SelectedRows)转 模型 Model的更多相关文章

  1. 关于Winform下DataGridView中实现checkbox全选反选、同步列表项的处理

    近期接手一个winform 项目,虽然之前有.net 的经验,但是对一些控件的用法还不是很熟悉. 这段时间将会记录一些在工作中遇到的坎坷以及对应的解决办法,写出来与大家分享并希望大神提出更好解决方法来 ...

  2. C#实现WinForm下DataGridView控件的拷贝和粘贴

    DataGridView控件应该是数据库应用系统最常用的控件之一,其方便性不言而喻的.往往用户在使用过程中会提出"从DataGridView空间 中拷贝数据或是向某个DataGridView ...

  3. WinForm Control - DataGridView

    http://blog.csdn.net/fangxing80/article/details/1561011 .NET 2.0 - WinForm Control - DataGridView 编程 ...

  4. Winform下CefSharp的引用、配置、实例与报错排除(源码)

    Winform下CefSharp的引用.配置.实例与报错排除 本文详细介绍了CefSharp在vs2013..net4.0环境下,创建Winfrom项目.引用CefSharp的方法,演示了winfro ...

  5. WinForm中DataGridView显示更新数据--人性版

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. c# WinForm开发 DataGridView各种操作总结大全

    一.单元格内容的操作 //取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index Consol ...

  7. c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...

  8. 转:c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index  ...

  9. winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色

    winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色 先设置奇数行颜色,这个有个自带的属性AlternatingRowsDefaultCellStyle ...

  10. 【接上一篇】winform中dataGridView高度和宽度自适应填充完数据的高度和宽度,即dataGridView根据数据自适应大小

    上一篇:winform中dataGridView高度自适应填充完数据的高度 winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度 ...

随机推荐

  1. cxGrid布局的保存和恢复

    //恢复布局IniFileName := ExtractFilePath(Application.ExeName) + 'Layout/' + Self.Name + '.ini';if FileEx ...

  2. 【自用】MySQL数据库基本操作

    docker 中下载 mysql docker pull mysql 启动 docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=12 ...

  3. js判断iOS还是Android

    /** * 运行设备引擎, 即iOS, Android还是H5 * 返回值注意大小写 * @return iOS, Android, H5 */ function engineType() { let ...

  4. 基于Surprise和Flask构建个性化电影推荐系统:从算法到全栈实现

    一.引言:推荐系统的魔法与现实意义 在Netflix每年节省10亿美元内容采购成本的背后,在YouTube占据用户80%观看时长的推荐算法中,推荐系统正悄然改变内容消费模式.本文将带您从零开始构建一个 ...

  5. 关于用nw(node-webkit)开发windows桌面软件遇到的问题,包括nw项目打包、win10 C盘读写文件没权限等问题的解决方法

    原文: 关于用nw(node-webkit)开发windows桌面软件遇到的问题,包括nw项目打包.win10 C盘读写文件没权限等问题的解决方法 - 搜栈网 (seekstack.cn)https: ...

  6. 三维装箱问题(3D Bin Packing Problem, 3D-BPP)

    提出问题 集装箱海运家具, 沙发, 茶几, 椅子等等, 有多少套家具,以及每个家具的长宽高都会告诉你. 把所有的家具都装进集装箱里, 要求通过算法算出一共需要多少集装箱. 1.要考虑怎样装, 需要的集 ...

  7. 【MOOC】华中科技大学操作系统慕课答案-第1~3章单元测试

    单选 1 下列说法错误的是 . A. 手工操作阶段,资源利用率低的原因是因为程序的准备和撤销都需要手工完成. B. 单道批处理系统中CPU和外设交替工作和空闲. √C. 单道批处理系统效率之所以比手工 ...

  8. C# 中 WebSocket 与 SignalR:实时通信的两种选择

    在现代 Web 应用中,实时通信变得越来越重要.无论是聊天应用.在线游戏.股票行情推送还是协作编辑工具,都需要服务器能够主动向客户端推送数据.在 .NET 生态系统中,WebSocket 和 Sign ...

  9. 开源PDF处理工具——Ghostscript的安装和使用

    1. 安装 Ghostscript Windows 下载 Ghostscript: 官网:https://www.ghostscript.com/download/gsdnld.html 选择适合你的 ...

  10. 洛谷题解:B4152 [厦门小学生 C++ 2022] 方阵排序

    题目传送门. 输入 #1 3 5 50 65 70 80 90 90 95 100 输出 #1 9 8 7 6 5 3 3 2 1 思路 把问题转化为求一个数在一堆数里的排名. 这时候直接用二分查找, ...