下列代码列示了一个 ColumnView.CustomDrawEmptyForeground 事件处理程序, 当没有记录在视图中呈现之时执行视图区域的自定义绘制。 在本例中,在视图的空白区域绘制一个字符串。

下面的插图展示了自定义绘制的结果。

C#CopyCode image复制代码
using DevExpress.XtraGrid.Views.Base;
using System.Drawing;

private void gridView1_CustomDrawEmptyForeground(object sender, CustomDrawEventArgs e) {
   string s;
   ColumnView view = sender as ColumnView;
   // Get the number of records in the underlying data source.
   DataView dataSource = view.DataSource as DataView;
   if(dataSource.Count == 0) //the data source is empty
      s = "Press Insert to add a new record";
   else //no records meet the filter criteria
      s = "No records match the current filter criteria";
   Font font = new Font("Tahoma", 10, FontStyle.Bold);
   Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5, 
     e.Bounds.Height - 5);
   e.Graphics.DrawString(s, font, Brushes.Black, r);
}


Visual BasicCopyCode image复制代码
Imports DevExpress.XtraGrid.Views.Base

Private Sub GridView1_CustomDrawEmptyForeground(ByVal sender As Object, _
  ByVal e As CustomDrawEventArgs) Handles GridView1.CustomDrawEmptyForeground
   Dim s As String
   Dim view As ColumnView = CType(sender, ColumnView)
   ' Get the number of records in the underlying data source.
   Dim dataSource As DataView = View.DataSource
   If dataSource.Count = 0 Then 'the data source is empty
      s = "Press Insert to add a new record"
   Else 'no records meet the filter criteria
      s = "No records match the current filter criteria"
   End If
   Dim font As Font = New Font("Tahoma", 10, FontStyle.Bold)
   Dim r As RectangleF = New RectangleF(e.Bounds.Left + 5, e.Bounds.Top + 5, _
     e.Bounds.Width - 5, e.Bounds.Height - 5)
   e.Graphics.DrawString(s, font, Brushes.Black, r)
End Sub