DevExpress控件使用经验之GridControl控件的使用

 

最近项目有用到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) 这样设置后,就能顺利显示行号了,是不是很方便呢。

posted @ 2013-08-22 12:03  技术_菜鸟  阅读(8937)  评论(1编辑  收藏  举报