WinForm实现基于BindingSource的方法扩展

 更新时间:2014年08月20日 08:47:39   投稿:shichen2014  
这篇文章主要介绍了WinForm实现基于BindingSource的方法扩展,需要的朋友可以参考下

本文实例展示了WinForm实现基于BindingSource的方法扩展,共享给大家供大家参考。具体方法如下:

关键代码如下:

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

namespace WinFormUtilHelpV2
{
  /// <summary>
  /// 基于.NET 2.0的BindingSource工具类
  /// </summary>
  public static class BindingSourceToolV2
  {
    /// <summary>
    /// 获取Control的BindingSource
    /// </summary>
    /// <param name="control">Control</param>
    /// <returns>BindingSource</returns>
    public static BindingSource GetBindingSource(this Control control)
    {
      if (control != null)
      {
        PropertyInfo _finded = control.GetType().GetProperty("DataSource");
        if (_finded != null)
        {
          object _dbsource = _finded.GetValue(control, null);
          if (_dbsource != null && _dbsource is BindingSource)
          {
            return _dbsource as BindingSource;
          }
        }
      }
      return null;
    }
    /// <summary>
    /// 从BindingSource中条件移出
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    /// <param name="dbSource">BindingSource</param>
    /// <param name="match">委托</param>
    /// <returns>条件移出个数</returns>
    public static int Remove<T>(this BindingSource dbSource, Predicate<T> match) where T : class
    {
      int _count = 0;
      if (dbSource != null)
      {
        for (int i = 0; i < dbSource.List.Count; i++)
        {
          object _cur = dbSource.List[i];
          if (match((T)_cur))
          {
            dbSource.List.Remove(_cur);
            _count++;
            i--;
          }
        }
      }
      return _count;
    }
    /// <summary>
    /// 从BindingSource中条件查找
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    /// <param name="dbSource">BindingSource</param>
    /// <param name="match">委托</param>
    /// <returns>没有查找到则返回NULL</returns>
    public static T Find<T>(this BindingSource dbSource, Predicate<T> match) where T : class
    {
      T _finded = null;
      if (dbSource != null)
      {
        foreach (T t in dbSource.List)
        {
          if (match(t))
          {
            _finded = t;
            break;
          }
        }
      }
      return _finded;
    }
    /// <summary>
    /// 从BindingSource中条件查找集合
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    /// <param name="dbSource">BindingSource</param>
    /// <param name="match">委托</param>
    /// <returns>没有查找到则返回NULL</returns>
    public static IList<T> FindAll<T>(this BindingSource dbSource, Predicate<T> match) where T : class
    {
      IList<T> _findedList = null;
      if (dbSource != null)
      {
        _findedList = new List<T>();
        foreach (T t in dbSource.List)
        {
          if (match(t))
          {
            _findedList.Add(t);
          }
        }
      }
      return _findedList;
    }

  }
}

测试代码如下:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using WinFormUtilHelpV2;
using WinFormUtilHelpV2Test.Models;
namespace WinFormUtilHelpV2Test
{
  public partial class WinBindingSourceToolV2Test : Form
  {
    public WinBindingSourceToolV2Test()
    {
      InitializeComponent();
    }
    private void WinBindingSourceToolV2Test_Load(object sender, EventArgs e)
    {
      IList<Person> _source = new List<Person>();
      for (int i = 0; i < 10; i++)
      {
        Person _entity = new Person();
        _entity.Age = i;
        _entity.Name = "YanZhiwei" + i;
        _source.Add(_entity);
      }
      dataGridView1.SetBindingSource(_source);
    }
    private void button1_Click(object sender, EventArgs e)
    {
      Person _person = dataGridView1.GetBindingSource().Find<Person>(c => c.Age == 5);
      MessageBox.Show("条件查找:" + _person != null ? "查找到一个." : "未查找到.");
    }
    private void button2_Click(object sender, EventArgs e)
    {
      int _count = dataGridView1.GetBindingSource().Remove<Person>(c => c.Age >= 5);
      MessageBox.Show("成功移出:" + _count);
    }
    private void button3_Click(object sender, EventArgs e)
    {
      IList<Person> _personList = dataGridView1.GetBindingSource().FindAll<Person>(c => c.Age < 5);
      MessageBox.Show("条件查找:" + _personList != null ? "查找到" + _personList.Count + "个" : "未查找到.");
    }
  }
}
    /// <summary>
    /// DataGridView SetBindingSource
    /// </summary>
    /// <typeparam name="T">IList</typeparam>
    /// <param name="dataGrid">dataGrid</param>
    /// <param name="source">泛型</param>
    public static void SetBindingSource<T>(this DataGridView dataGrid, IList<T> source)
    {
      BindingList<T> _bindinglist = new BindingList<T>(source);
      BindingSource _source = new BindingSource(_bindinglist, null);
      dataGrid.DataSource = _source;
    }

测试结果如下图所示:

希望本文所述实例对大家C#程序设计能有所帮助!

相关文章

  • c#实现适配器模式的项目实践

    c#实现适配器模式的项目实践

    适配器模式将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作,本文主要介绍了c#实现适配器模式的项目实践,感兴趣的可以一起来了解一下
    2023-08-08
  • C#中把Json数据转为DataTable

    C#中把Json数据转为DataTable

    这篇文章介绍了C#中把Json数据转为DataTable的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#事件标准命名规则及说明(包括用作事件类型的委托命名)

    C#事件标准命名规则及说明(包括用作事件类型的委托命名)

    这篇文章主要介绍了C#事件标准命名规则及说明(包括用作事件类型的委托命名),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C#编程实现四舍五入、向上及下取整的方法

    C#编程实现四舍五入、向上及下取整的方法

    这篇文章主要介绍了C#编程实现四舍五入、向上及下取整的方法,涉及C#数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • C#处理JPEG头信息的方法

    C#处理JPEG头信息的方法

    相信大家肯定都看过用C或C++处理JPEG头信息的程序了,我也看了,不过因为我不懂C,看得我头疼。所以我还是决定用C#来写吧
    2013-05-05
  • c# 给pdf添加数字签名的步骤

    c# 给pdf添加数字签名的步骤

    这篇文章主要介绍了c# 给pdf添加数字签名的步骤,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-12-12
  • C#创建简单windows窗体应用(加法器)

    C#创建简单windows窗体应用(加法器)

    这篇文章主要为大家详细介绍了C#创建一个简单windows窗体应用的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C# GDI+实现时钟表盘

    C# GDI+实现时钟表盘

    这篇文章主要为大家详细介绍了C# GDI+实现时钟表盘,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • C#之CLR内存字符串常量池(string)

    C#之CLR内存字符串常量池(string)

    这篇文章主要介绍了C#之CLR内存字符串常量池(string),对于学习和理解C#内存原理很有帮助,需要的朋友可以参考下
    2014-08-08
  • C#函数式编程中的部分应用详解

    C#函数式编程中的部分应用详解

    这篇文章主要介绍了C#函数式编程中的部分应用详解,本文讲解了何谓函数式编程、函数式编程中的部分应用是什么、部分应用的代码实例,需要的朋友可以参考下
    2015-01-01

最新评论