// *********************************************************************** // Assembly : HZH_Controls // Created : 08-28-2019 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace HZH_Controls.Controls { /// /// Class UCComboxGridPanel. /// Implements the /// /// [ToolboxItem(false)] public partial class UCComboxGridPanel : UserControl { /// /// 项点击事件 /// [Description("项点击事件"), Category("自定义")] public event DataGridViewEventHandler ItemClick; /// /// The m row type /// private Type m_rowType = typeof(UCDataGridViewRow); /// /// 行类型 /// /// The type of the row. [Description("行类型"), Category("自定义")] public Type RowType { get { return m_rowType; } set { m_rowType = value; this.ucDataGridView1.RowType = m_rowType; } } /// /// The m columns /// private List m_columns = null; /// /// 列 /// /// The columns. [Description("列"), Category("自定义")] public List Columns { get { return m_columns; } set { m_columns = value; this.ucDataGridView1.Columns = value; } } /// /// The m data source /// private List m_dataSource = null; /// /// 数据源 /// /// The data source. [Description("数据源"), Category("自定义")] public List DataSource { get { return m_dataSource; } set { m_dataSource = value; } } /// /// The string last search text /// private string strLastSearchText = string.Empty; /// /// Initializes a new instance of the class. /// public UCComboxGridPanel() { InitializeComponent(); this.txtSearch.txtInput.TextChanged += txtInput_TextChanged; this.ucDataGridView1.ItemClick += ucDataGridView1_ItemClick; m_page.ShowSourceChanged += m_page_ShowSourceChanged; } /// /// Handles the ItemClick event of the ucDataGridView1 control. /// /// The source of the event. /// The instance containing the event data. void ucDataGridView1_ItemClick(object sender, DataGridViewEventArgs e) { if (ItemClick != null) { ItemClick((sender as IDataGridViewRow).DataSource, null); } } /// /// Handles the TextChanged event of the txtInput control. /// /// The source of the event. /// The instance containing the event data. void txtInput_TextChanged(object sender, EventArgs e) { timer1.Enabled = false; timer1.Enabled = true; } /// /// Handles the Load event of the UCComboxGridPanel control. /// /// The source of the event. /// The instance containing the event data. private void UCComboxGridPanel_Load(object sender, EventArgs e) { m_page.DataSource = m_dataSource; this.ucDataGridView1.DataSource = m_page.GetCurrentSource(); } /// /// Handles the Tick event of the timer1 control. /// /// The source of the event. /// The instance containing the event data. private void timer1_Tick(object sender, EventArgs e) { if (strLastSearchText == txtSearch.InputText) { timer1.Enabled = false; } else { strLastSearchText = txtSearch.InputText; Search(txtSearch.InputText); } } /// /// Searches the specified string text. /// /// The string text. private void Search(string strText) { m_page.StartIndex = 0; if (!string.IsNullOrEmpty(strText)) { strText = strText.ToLower().Trim(); List lst = m_dataSource.FindAll(p => m_columns.Any(c => (c.Format == null ? (p.GetType().GetProperty(c.DataField).GetValue(p, null).ToStringExt()) : c.Format(p.GetType().GetProperty(c.DataField).GetValue(p, null))).ToLower().Contains(strText))); m_page.DataSource = lst; } else { m_page.DataSource = m_dataSource; } m_page.Reload(); } void m_page_ShowSourceChanged(object currentSource) { this.ucDataGridView1.DataSource = currentSource; } } }