UCComboxGridPanel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-28-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCComboxGridPanel.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCComboxGridPanel.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [ToolboxItem(false)]
  32. public partial class UCComboxGridPanel : UserControl
  33. {
  34. /// <summary>
  35. /// 项点击事件
  36. /// </summary>
  37. [Description("项点击事件"), Category("自定义")]
  38. public event DataGridViewEventHandler ItemClick;
  39. /// <summary>
  40. /// The m row type
  41. /// </summary>
  42. private Type m_rowType = typeof(UCDataGridViewRow);
  43. /// <summary>
  44. /// 行类型
  45. /// </summary>
  46. /// <value>The type of the row.</value>
  47. [Description("行类型"), Category("自定义")]
  48. public Type RowType
  49. {
  50. get { return m_rowType; }
  51. set
  52. {
  53. m_rowType = value;
  54. this.ucDataGridView1.RowType = m_rowType;
  55. }
  56. }
  57. /// <summary>
  58. /// The m columns
  59. /// </summary>
  60. private List<DataGridViewColumnEntity> m_columns = null;
  61. /// <summary>
  62. /// 列
  63. /// </summary>
  64. /// <value>The columns.</value>
  65. [Description("列"), Category("自定义")]
  66. public List<DataGridViewColumnEntity> Columns
  67. {
  68. get { return m_columns; }
  69. set
  70. {
  71. m_columns = value;
  72. this.ucDataGridView1.Columns = value;
  73. }
  74. }
  75. /// <summary>
  76. /// The m data source
  77. /// </summary>
  78. private List<object> m_dataSource = null;
  79. /// <summary>
  80. /// 数据源
  81. /// </summary>
  82. /// <value>The data source.</value>
  83. [Description("数据源"), Category("自定义")]
  84. public List<object> DataSource
  85. {
  86. get { return m_dataSource; }
  87. set { m_dataSource = value; }
  88. }
  89. /// <summary>
  90. /// The string last search text
  91. /// </summary>
  92. private string strLastSearchText = string.Empty;
  93. /// <summary>
  94. /// Initializes a new instance of the <see cref="UCComboxGridPanel" /> class.
  95. /// </summary>
  96. public UCComboxGridPanel()
  97. {
  98. InitializeComponent();
  99. this.txtSearch.txtInput.TextChanged += txtInput_TextChanged;
  100. this.ucDataGridView1.ItemClick += ucDataGridView1_ItemClick;
  101. m_page.ShowSourceChanged += m_page_ShowSourceChanged;
  102. }
  103. /// <summary>
  104. /// Handles the ItemClick event of the ucDataGridView1 control.
  105. /// </summary>
  106. /// <param name="sender">The source of the event.</param>
  107. /// <param name="e">The <see cref="DataGridViewEventArgs" /> instance containing the event data.</param>
  108. void ucDataGridView1_ItemClick(object sender, DataGridViewEventArgs e)
  109. {
  110. if (ItemClick != null)
  111. {
  112. ItemClick((sender as IDataGridViewRow).DataSource, null);
  113. }
  114. }
  115. /// <summary>
  116. /// Handles the TextChanged event of the txtInput control.
  117. /// </summary>
  118. /// <param name="sender">The source of the event.</param>
  119. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  120. void txtInput_TextChanged(object sender, EventArgs e)
  121. {
  122. timer1.Enabled = false;
  123. timer1.Enabled = true;
  124. }
  125. /// <summary>
  126. /// Handles the Load event of the UCComboxGridPanel control.
  127. /// </summary>
  128. /// <param name="sender">The source of the event.</param>
  129. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  130. private void UCComboxGridPanel_Load(object sender, EventArgs e)
  131. {
  132. m_page.DataSource = m_dataSource;
  133. this.ucDataGridView1.DataSource = m_page.GetCurrentSource();
  134. }
  135. /// <summary>
  136. /// Handles the Tick event of the timer1 control.
  137. /// </summary>
  138. /// <param name="sender">The source of the event.</param>
  139. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  140. private void timer1_Tick(object sender, EventArgs e)
  141. {
  142. if (strLastSearchText == txtSearch.InputText)
  143. {
  144. timer1.Enabled = false;
  145. }
  146. else
  147. {
  148. strLastSearchText = txtSearch.InputText;
  149. Search(txtSearch.InputText);
  150. }
  151. }
  152. /// <summary>
  153. /// Searches the specified string text.
  154. /// </summary>
  155. /// <param name="strText">The string text.</param>
  156. private void Search(string strText)
  157. {
  158. m_page.StartIndex = 0;
  159. if (!string.IsNullOrEmpty(strText))
  160. {
  161. strText = strText.ToLower().Trim();
  162. List<object> 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)));
  163. m_page.DataSource = lst;
  164. }
  165. else
  166. {
  167. m_page.DataSource = m_dataSource;
  168. }
  169. m_page.Reload();
  170. }
  171. void m_page_ShowSourceChanged(object currentSource)
  172. {
  173. this.ucDataGridView1.DataSource = currentSource;
  174. }
  175. }
  176. }