轉:http://www.cnblogs.com/jaxu/archive/2011/08/04/2127365.html
我們見過Excel中的數據過濾功能,可以通過點擊表頭上的下拉列表來實現數據的過濾,這個功能很實用,省去了我們需要在程序中單獨設計數據的查詢過濾模塊,功能直接依賴於數據綁定控件DataGridView。先來看看Excel中的數據過濾功能。
要想在DataGridView中實現類似於Excel的這種功能其實也並非難事。來看看msdn上的一篇文章,上面有詳細的介紹,不過目前只有全英文的版本。http://msdn.microsoft.com/en-us/library/aa480727.aspx。里面提供的下載示例我這里也可以提供一份:DataGridViewAutoFilter.zip
文章講了很多有關如何實現數據過濾的知識,如果你有耐心可以通讀一遍,應該能有不小的收獲。其實這里面的原理就是我們需要自定義一種DataGridViewColumn,它能支持用戶通過點擊表頭上的下拉列表來實現DataGridView的數據過濾。自定義的DataGridViewColumn可以繼承自現有的DataGridViewTextBoxColumn類型,另外還需要自定義一個繼承自DataGridViewColumnHeaderCell的類型,它負責在DataGridView表頭上呈現一個下拉列表,並完成數據過濾的選擇功能。下載上面的DataGridViewAutoFilter.zip壓縮包,將里面對應編程語言中的DataGridViewAutoFilterColumnHeaderCell.cs和DataGridAutoFilterTextBoxColumn.cs兩個文件加入到你的工程中。然后需要重新定義DataGridView中的列,如果你是手動指定DataGridView的列,則需要在窗體的Designer.cs文件中手動修改與DataGridView列相關的代碼;或者你也可以通過程序動態指定DataGridView的列。將需要顯示數據過濾的列的類型指定為DataGridViewAutoFilterTextBoxColumn類型。另外在綁定DataGridView數據源時必須使用BindingSource而不能使用如DataTable之類的普通數據源,這一點非常重要!在后面的代碼展示中你將會看到為什么要這么做。
這里是具體的例子:
public Form1() { InitializeComponent(); // create sequence Item[] items = new Item[] { new Book{Id = 1, Price = 13.50, Genre = "Comedy", Author = "Jim Bob"}, new Book{Id = 2, Price = 8.50, Genre = "Drama", Author = "John Fox"}, new Movie{Id = 1, Price = 22.99, Genre = "Comedy", Director = "Phil Funk"}, new Movie{Id = 1, Price = 13.40, Genre = "Action", Director = "Eddie Jones"}}; var query = from i in items orderby i.Price select i; DataTable table = query.CopyToDataTable(); BindingSource source = new BindingSource(); source.DataSource = table; foreach (DataColumn col in table.Columns) { DataGridViewAutoFilterTextBoxColumn commonColumn = new DataGridViewAutoFilterTextBoxColumn(); commonColumn.DataPropertyName = col.ColumnName; commonColumn.HeaderText = col.ColumnName; commonColumn.Resizable = DataGridViewTriState.True; this.dataGridView1.Columns.Add(commonColumn); } this.dataGridView1.DataSource = source; }
代碼中的第16行將LINQ的查詢結果轉換成了DataTable對象,相關內容大家可以看我的另一篇文章“如何將LINQ查詢到的結果由匿名類型var轉換成DataTable對象”。另外代碼中將DataGridView的所有列的類型指定成了DataGridViewAutoFilterTextBoxColumn,使其能夠支持自定義的數據過濾功能。好了,現在運行你的應用程序,將會看到表頭上有下拉列表的小箭頭,點擊它並選擇下拉列表中的項便可實現DataGridView數據的排序。是不是很酷啊?不過這里還有一個小問題,那就是用戶如何知道我當前選擇了哪個列的數據過濾,界面是不是應該給出相應的數據過濾信息呢?我們可以在窗體的StatusStrip控件中添加一些Label標簽用來顯示這些信息:
1. 顯示用戶當前選擇了多少行。這個需要將DataGridView的SelectionMode屬性設置成行選擇模式即FullRowSelect。
2. 顯示當前DataGridView一共有多少行。
3. 顯示Filter的信息及應用數據過濾之后的總行數。
4. 添加一個按鈕或鏈接用於移除當前的Filter。
來看看具體的實現代碼及程序運行時的效果:
private void dataGridView1_SelectionChanged(object sender, EventArgs e) { int iCount = this.dataGridView1.SelectedRows.Count; this.toolStripStatus_SelectedRows.Text = string.Format("{0} row{1} selected", iCount.ToString(), iCount > 1 ? "s" : ""); } private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { BindingSource data = this.dataGridView1.DataSource as BindingSource; if (data == null || data.DataSource == null) { return; } /* Show total records number*/ // Retrieve the unfiltered row count by // temporarily unfiltering the data. data.RaiseListChangedEvents = false; String oldFilter = data.Filter; data.Filter = null; int iTotalNum = data.Count; this.toolStripStatus_Total.Text = string.Format("Total of {0} record{1}.", iTotalNum.ToString(), iTotalNum > 1 ? "s" : ""); data.Filter = oldFilter; data.RaiseListChangedEvents = true; /* Show filter information.*/ int iFilterNum = data.Count; string filter = data.Filter; if (String.IsNullOrEmpty(filter)) { this.toolStripStatus_Separator2.Visible = false; this.toolStripStatus_Filter.Visible = false; this.toolStripStatus_ShowAll.Visible = false; } else { this.toolStripStatus_Separator2.Visible = true; this.toolStripStatus_Filter.Visible = true; this.toolStripStatus_ShowAll.Visible = true; this.toolStripStatus_Filter.Text = string.Format("{0} record{1} found.", iFilterNum.ToString(), iFilterNum > 1 ? "s" : ""); this.toolStripStatus_Filter.Text += " (Filter: " + filter + ")"; } } private void toolStripStatus_ShowAll_Click(object sender, EventArgs e) { DataGridViewAutoFilterColumnHeaderCell.RemoveFilter(this.dataGridView1); }
1. 當前用戶選擇的總行數。
2. DataGridView中一共有多少行。
3. Filter的信息及使用Filter之后的數據行數。
4. 用於移除Filter的鏈接。
代碼中一共是三個事件,dataGridView1_SelectionChanged事件用於在DataGridView行被選擇時觸發,用來更新StatusStrip中當前用戶選擇的總行數;dataGridView1_DataBindingComplete事件在DataGridView數據完成綁定時觸發,用來更新StatusStrip中Filter的信息及使用Filter之后的數據行數,以及DataGridView的數據總行數,注意其中將BindingSource的RaiseListChangedEvents設置為false以取得DataGridView數據源中的真實數據行數,之后再將其設置為true以獲取到Filter的相關信息;toolStripStatus_ShowAll_Click事件為用戶點擊Show All鏈接時觸發,用於移除DataGridView中的Filter。
這里是完整的代碼:
http://pan.baidu.com/s/1eRZiw8M