最近項目有用到DevExpress這個插件,但我發現DevExpress這個插件雖然好用,但性能不咋的,然后也比較少中文資料。
但不管咋樣,既然項目用到,只能硬着頭皮上了。
首先我介紹一下GridControl控件的使用,更多其他控件使用,請繼續關注本博客,或者直接跟我聯系吧。
首先介紹GridControl控件之前,給大家上個圖。
如上兩圖所示,Dev列表控件GridControl默認的格式並沒有漸變變色效果,顯示的日期數據,也是“yyyy-MM-dd”的格式,而非“yyyy-MM-dd HH:mm:ss”即使對於后面有長格式的日期數據也一樣。下面分別對這兩種情況進行說明。
如上兩圖所示,我們有時候需要控制列表訪問過的顏色變化,或者是時間顯示格式等內容,這個時候設置GridView的RowCellStyle即可實現,如下所示。
1 this.gridView1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle); 2 3 void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) 4 { 5 if (e.Column.FieldName == "PublishType") 6 { 7 if (e.CellValue != null && e.CellValue.ToString() == "中介") 8 { 9 e.Appearance.BackColor = Color.DeepSkyBlue; 10 e.Appearance.BackColor2 = Color.LightCyan; 11 } 12 } 13 if (e.Column.FieldName == "PublishTime") 14 { 15 e.Column.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss"; 16 } 17 if (e.Column.FieldName == "Title") 18 { 19 string id = this.winGridViewPager1.gridView1.GetRowCellDisplayText(e.RowHandle, "Id"); 20 if (historyDict.ContainsKey(id)) 21 { 22 e.Appearance.BackColor = Color.DeepSkyBlue; 23 e.Appearance.BackColor2 = Color.LightCyan; 24 } 25 } 26 }
對於日期格式,如果需要列表中的日期小於某個值(如1900-1-1)的時候,就設置為空,那么可以通過下面的方法進行設置即可解決。
1 this.winGridViewPager1.gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(gridView1_CustomColumnDisplayText); 2 3 void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) 4 { 5 if (e.Column.ColumnType == typeof(DateTime)) 6 { 7 string columnName = e.Column.FieldName; 8 if (e.Value != null && Convert.ToDateTime(e.Value) <= 9 Convert.ToDateTime("1900-1-1")) 10 { 11 e.DisplayText = ""; 12 } 13 } 14 }
至於GridControl中的GridView內容打印。
由於GridView的良好封裝性,實現打印的代碼很簡單。
1 private void menu_Print_Click(object sender, EventArgs e) 2 { 3 PrintableComponentLink link = new PrintableComponentLink(new PrintingSystem()); 4 link.Component = this.gridControl1; 5 link.Landscape = true; 6 link.PaperKind = System.Drawing.Printing.PaperKind.A3; 7 link.CreateMarginalHeaderArea += new 8 CreateAreaEventHandler(Link_CreateMarginalHeaderArea); 9 link.CreateDocument(); 10 link.ShowPreview(); 11 } 12 private void Link_CreateMarginalHeaderArea(object sender, CreateAreaEventArgs e) 13 { 14 string title = string.Format("年度大體檢-({0}年度)", this.txtYear.Text); 15 PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.None, title, Color.DarkBlue, 16 new RectangleF(0, 0, 100, 21), BorderSide.None); 17 brick.LineAlignment = BrickAlignment.Center; 18 brick.Alignment = BrickAlignment.Center; 19 brick.AutoWidth = true; 20 brick.Font = new System.Drawing.Font("宋體", 11f, FontStyle.Bold); 21 }
設置GridView的行指示器(行頭)顯示行號
在我的分頁控件以及Winform開發框架很多項目介紹里面,很多都顯示了行號,其實這個在DevExpress中的實現很簡單,如果需要,可以實現在自己的代碼里面。
1) 先實現GridView的CustomDrawRowIndicator事件,實現代碼如下所示。
1 private void advBandedGridView1_CustomDrawRowIndicator(object sender, 2 DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) 3 { 4 e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far; 5 if (e.Info.IsRowIndicator) 6 { 7 if (e.RowHandle >= 0) 8 { 9 e.Info.DisplayText = (e.RowHandle + 1).ToString(); 10 } 11 else if (e.RowHandle < 0 && e.RowHandle > -1000) 12 { 13 e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite; 14 e.Info.DisplayText = "G" + e.RowHandle.ToString(); 15 } 16 } 17 }
2) 然后設置GridView控件的IndicatorWidth為合適的寬度,如40左右則比較好。
3) 這樣設置后,就能順利顯示行號了,是不是很方便呢。