C#实现WinForm DataGridView控件支持叠加数据绑定

C#实现WinForm DataGridView控件支持叠加数据绑定

我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支持,而不是实现,是因为他既可以是实现了IListSource的类型,也可以是实现了IList的类型,例如:List类型,DataTable类型等,这里就不一一列举了,今天我主要实现的功能如标题所描述的:实现WinForm DataGridView控件支持叠加数据绑定,或者说是附加数据功能,什么意思呢?说白了就是支持数据的多次绑定,标准的绑定方法只支持单一绑定,即每次绑定均会清除原来的数据,而叠加数据绑定则可实现每次绑定均以附加的形式(原数据保留)添加到DataGridView控件中,这样就实现了分页加载,但可完整显示已加载的所有数据,这种应用场景在C/S端很常见,B/S端上也有(例如QQ空间动态下面的加载更多按钮)

以下是实现附加数据两种方式:

第一种方式,采用反射获取属性值并循环添加数据行

        private static void AppendDataToGrid(DataGridView grid, IList<object> source)
        {
            int rowCount = grid.Rows.Count;
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            Type t = source[0].GetType();
            int rowIndex = grid.Rows.Add();
            var girdCells = grid.Rows[rowIndex].Cells;
            //Common.ShowProcessing("正在加载数据,请稍候...", Common.MainForm, (o) =>
            //{
    
                foreach (object item in source)
                {

                    var row = new DataGridViewRow();
                    foreach (DataGridViewCell cell in girdCells)
                    {
                        var p = t.GetProperty(cell.OwningColumn.DataPropertyName);
                        object pValue = p.GetValue(item, null);
                        var newCell = (DataGridViewCell)cell.Clone();
                        newCell.Value = pValue;
                        row.Cells.Add(newCell);
                    }
                    rows.Add(row);
                }
            //});

            grid.Rows.RemoveAt(rowIndex);
            grid.Rows.AddRange(rows.ToArray());

        }

每二种方式,采用将数据源合并,然后重新绑定

        protected void AppendDataToGrid<T,TResult>(DataGridView dataGridBase, IList<T> source,Func<T,TResult> orderBy) where T : class
        {
            //Stopwatch watch = new Stopwatch();
            //watch.Start();

            if (dataGridBase.Rows.Count > 0)
            {
                IEnumerable<T> bindsource = null;
                Common.ShowProcessing("正在加载数据,请稍候...", Common.MainForm, (o) =>
                    {
                        var oldSource = (IList<T>)dataGridBase.DataSource;
                        bindsource = source.Concat(oldSource).OrderBy(orderBy).ToList();
                    });
                dataGridBase.DataSource = bindsource;
            }
            else
            {
                dataGridBase.DataSource = source;
            }

            //watch.Stop();
            //MessageBox.Show(watch.ElapsedMilliseconds.ToString());

        }

以上两种方法在代码量来看,第二种比较简单,第一种在执行效率上相对第二种方法要高,原因很简单,第一种每次处理的数据永远都是每页的数据,而第二种每次处理的数据是原有数据与现有数据的合集,随着数据量越多,加载也就越慢,大家也可以试一下,当然如果大家有其它更好的方法也可以分享一下。

为了体现面向对象以及可复用性,我将上述方法变为扩展方法,完整代码如下:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data;

namespace Zwj.Demo
{
    public interface IAppendDataAble<out TControl> where TControl : Control
    {

    }

    public class DataGridView2 : DataGridView, IAppendDataAble<DataGridView>
    {

    }

    public static class AppendDataAbleControlExtension
    {
        public static void AppendData(this DataGridView grid, dynamic dataSource)
        {
            if (!(grid is IAppendDataAble<DataGridView>))
            {
                throw new Exception("该DataGridView控件未实现IAppendDataAble<DataGridView>,无法使用该方法!");
            }

            if (dataSource.GetType().IsValueType || dataSource == null)
            {
                grid.DataSource = null;
                return;
            }

       Type interfaceType=dataSource.GetType().GetInterface("System.Collections.IList", true);
if (interfaceType!=null) {

          List<object> list = new List<object>();
          list.AddRange(dataSource);
          AppendDataToGrid(grid, list);

            }
            else if (dataSource is DataTable)
            {
                AppendDataToGrid(grid, dataSource as DataTable);
            }
        }

        /// <summary>
        /// 附加数据到DataGridView(支持IList<T>类型的数据源)
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="source"></param>
        private static void AppendDataToGrid(DataGridView grid, IList<object> source)
        {
            int rowCount = grid.Rows.Count;
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            Type t = source[0].GetType();
            int rowIndex = grid.Rows.Add();
            var girdCells = grid.Rows[rowIndex].Cells;
            //Common.ShowProcessing("正在加载数据,请稍候...", Common.MainForm, (o) =>
            //{
    
            foreach (object item in source)
            {

                var row = new DataGridViewRow();
                foreach (DataGridViewCell cell in girdCells)
                {
                    var p = t.GetProperty(cell.OwningColumn.DataPropertyName);
                    object pValue = p.GetValue(item, null);
                    var newCell = (DataGridViewCell)cell.Clone();
                    newCell.Value = pValue;
                    row.Cells.Add(newCell);
                }
                rows.Add(row);
            }
            //});

            grid.Rows.RemoveAt(rowIndex);
            grid.Rows.AddRange(rows.ToArray());

        }

        /// <summary>
        /// 附加数据到DataGridView(支持DataTable类型的数据源)
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="table"></param>
        private static void AppendDataToGrid(DataGridView grid, DataTable table)
        {
            int rowCount = grid.Rows.Count;
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            int rowIndex = grid.Rows.Add();
            var girdCells = grid.Rows[rowIndex].Cells;
            //Common.ShowProcessing("正在加载数据,请稍候...", Common.MainForm, (o) =>
            //{
    
            foreach (DataRow r in table.Rows)
            {
                var row = new DataGridViewRow();
                foreach (DataGridViewCell cell in girdCells)
                {
                    object pValue = r[cell.OwningColumn.DataPropertyName];
                    var newCell = (DataGridViewCell)cell.Clone();
                    newCell.Value = pValue;
                    row.Cells.Add(newCell);
                }
                rows.Add(row);
            }
            //});

            grid.Rows.RemoveAt(rowIndex);
            grid.Rows.AddRange(rows.ToArray());
        }

    }
}

对代码稍微说明一下,为了避免扩展方法被滥用,即不需要附加数据的普通DataGridView造成影响,我定义了一个接口来规范它:IAppendDataAble<out TControl>,当然这个接口适用于所有控件,然后在扩展方法时AppendData加判断,如果实现了IAppendDataAble接口,则表明需要用到附加数据功能,就进行后面的处理,否则报错。我这里是基于DataGridView来扩展,大家也可以基于我定义的DataGridView2来扩展,这样更方便。另外,我上面实现了针对两种数据源类型进行了分别处理,以满足大多数的情况。

方法种注释掉的方法是我写的显示遮罩层的方法,如果大家需要,可以查看我的这篇博文:Winform应用程序实现通用遮罩层

 使用方法如下:

1.添加DataGridView控件,然后将DataGridView类型更改为DataGridView2类型,当然如果大家不需要进行扩展约束,那就无需更改DataGridView控件类型。

2.设置DataGridView列,将列的DataPropertyName设置为需要绑定的数据字段名称,这步很重要。

3.然后查询数据并调用扩展方法:

//dataGridView2Demo为DataGridView2类型
//dataSource为查询到的数据
dataGridView2Demo.AppendData(dataSource);

 为了提高扩展方法的执行效率,降低数据源类型判断及转换,我们也可以选择将扩展方法直接分为两个扩展方法,如下:

    public static class ControlExtension
    {
        /// <summary>
        /// 附加数据到DataGridView(支持IList<T>类型的数据源)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="grid"></param>
        /// <param name="source"></param>
        public static void AppendData<T>(this DataGridView grid, IList<T> source) where T : class
        {
            int rowCount = grid.Rows.Count;
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            Type t = typeof(T);
            int rowIndex = grid.Rows.Add();
            var girdCells = grid.Rows[rowIndex].Cells;
            Common.ShowProcessing("正在加载数据,请稍候...", Common.MainForm, (o) =>
            {
                foreach (object item in source)
                {

                    var row = new DataGridViewRow();
                    foreach (DataGridViewCell cell in girdCells)
                    {
                        var p = t.GetProperty(cell.OwningColumn.DataPropertyName);
                        object pValue = p.GetValue(item, null);
                        var newCell = (DataGridViewCell)cell.Clone();
                        newCell.Value = pValue;
                        row.Cells.Add(newCell);
                    }
                    rows.Add(row);
                }
            });

            grid.Rows.RemoveAt(rowIndex);
            grid.Rows.AddRange(rows.ToArray());

        }

        /// <summary>
        ///  附加数据到DataGridView(支持DataTable类型的数据源)
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="table"></param>
        public static void AppendData(this DataGridView grid, DataTable table)
        {
            int rowCount = grid.Rows.Count;
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            int rowIndex = grid.Rows.Add();
            var girdCells = grid.Rows[rowIndex].Cells;
            Common.ShowProcessing("正在加载数据,请稍候...", Common.MainForm, (o) =>
            {
                foreach (DataRow r in table.Rows)
                {
                    var row = new DataGridViewRow();
                    foreach (DataGridViewCell cell in girdCells)
                    {
                        object pValue = r[cell.OwningColumn.DataPropertyName];
                        var newCell = (DataGridViewCell)cell.Clone();
                        newCell.Value = pValue;
                        row.Cells.Add(newCell);
                    }
                    rows.Add(row);
                }
            });

            grid.Rows.RemoveAt(rowIndex);
            grid.Rows.AddRange(rows.ToArray());
        }

    }

使用方法不变,至于用哪一种根据大家的喜好!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109478.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 《算法图解》-9动态规划 背包问题,行程最优化

    《算法图解》-9动态规划 背包问题,行程最优化本文属于《算法图解》系列。学习动态规划,这是一种解决棘手问题的方法,它将问题分成小问题,并先着手解决这些小问题。一背包问题背包问题,在可装物品有限的前提下,尽量装价值最大的物品,如果物品数量足够大,简单的暴力穷举法是不可行的O(2ⁿ),前一章介绍了《贪婪算法》就是解决如何找到近似解,这接近最优解,但可能不是最优解。如何找到最优解呢?就是动态规划算法。动态规划先解决子问题,…

    2022年7月26日
    6
  • WebApp开发-Google官方教程

    WebApp开发-Google官方教程概览你可以使用viewport的元数据、CSS和Javascript来为不同分辨率的屏幕设置合适的页面本文档中的技术适用于Android 2.0及以上设备,针对默认的Android Browser中及在WebView中呈现的页面如果你在为Android开发Web应用或者在为移动设备重新设计一个Web应用,你需要仔细考虑在不同设备上你的页面看起来是怎样的。因为Android设备有不同款型

    2022年6月17日
    35
  • 两周搞定计算机专业毕业设计,附源码+论文+答辩

    两周搞定计算机专业毕业设计,附源码+论文+答辩毕设+论文+答辩通关法则,看这一篇,源码都在下面了!写在前面,尽管论文方法和毕设源码都有,但还是要看课+自己敲完完善好。做好充分准备面对答辩。文章目录一、毕设二、论文2.1论文标题2.2确定论文大纲2.3论文内容2.3.1文科找文献:2.3.2论文摘要2.3.3绪论2.3.4论文内容2.4论文查重三、答辩3.1论文答辩前的准备3.2论文答辩现场对弈一、毕设毕设直通车《黑马智慧物业毕业设计》【毕设项目】01网上购物商城(前端+后端+云部署)【毕设项目】02网上购物商城(前端

    2022年7月26日
    9
  • BitBlt参数详解[通俗易懂]

    BitBlt参数详解[通俗易懂]对BitBlt()这个函数的最后一个参数的意义一直不是太了解,只会使用SRCCOPY,最近的一个项目使用到了这个函数,但是要求要背景透明的将源绘制到目标区域上,源是背景色和字,怎么只拷贝字而把背景色透明化呢??我的解决方法是,把源的背景色绘制为白色,字为黑色,然后在BitBlt的时候最后一个参数用SRCAND,果然可以达到我要的效果,这是为什么呢?呵呵趁此机会好好看看这个参数介绍吧~~开始之前,首先要明白,绘制其实就是在给每一个像素点涂颜色,每种颜色都是由红蓝黄三要素组合而成,因此通过RGB颜色值可以

    2022年10月18日
    0
  • 微积分公式大全(24个基本积分公式)

    微积分公式大全PDF下载地址:http://pan.baidu.com/s/1i5kl3CD

    2022年4月14日
    207
  • Jprofiler 7.2.3 : The network connection has been lost. The JVM has terminated unexpectedly

    Jprofiler 7.2.3 : The network connection has been lost. The JVM has terminated unexpectedlyJProfiler是一个商业授权的Java剖析工具,由EJ技术有限公司,针对的JavaEE和JavaSE应用程序开发的。它把CPU、执行绪和内存的剖析组合在一个强大的应用中。JProfiler可提供许多IDE整合和应用服务器整合用途。JProfiler的是一个独立的应用程序,是Eclipse软件的插件。它允许两个内存剖面评估内存使用情况和动态分配泄漏和CPU剖析,以评估线程冲突

    2022年5月12日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号