項目中需要對一個DataGridView控件進行類似於Excel查找的功能,之前是使用的DevExpress里面的DataGrid,用起來倒是很方便,它的列頭可以和Excel一樣進行隨意的篩選,但是那個是收費的東東,我用了幾天破解版的,擔心以后會有影響所以還是決定換掉它,VS自帶的DataGridView跟DevExpress里面的DataGrid相比確實相差太遠了,樣式不好看不說,功能上也欠缺了很多,為了滿足用戶的需求只得做一個查找定位的功能出來勉強滿足一下用戶的需求,
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Runtime.InteropServices; 10 11 namespace MES.Common 12 { 13 public partial class UserControlFind : UserControl 14 { 15 public UserControlFind() 16 { 17 InitializeComponent(); 18 } 19 20 //增加 一個事件,項目里面用到這個控件的時候就可以使用這個事件了。 21 [EditorBrowsable(EditorBrowsableState.Always)] 22 [Browsable(true)] 23 public event EventHandler U_Click; 24 25 public void btnFindValue_Click(object sender, EventArgs e) 26 { 27 if (U_Click != null) 28 U_Click(this, e); 29 } 30 } 31 }
1 UserControlFind u = new UserControlFind(); 2 3 //Find_Grid就是用戶控件里面的查找方法,這里直接調用它並傳入一個字符串和一個DataGridView 4 u.Find_Grid(userControlFind_OP.txtValue.Text.Trim(), this.dgv);
如果需要操作用戶控件里面其它控件,那么該控件的Modifiers屬性需要設置為Public才可以。
效果如圖: