Example E761
Visible to All Users

WinForms Data Grid - Draw empty rows

This example creates a custom grid control that displays empty rows below data rows:

WinForms Data Grid - Draw empty rows

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Form1.cs(vb)
C#
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using CustomGrid; using DevExpress.XtraEditors; namespace EmptyAreaGridLines { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private MyGridControl myGridControl1; private System.Data.DataSet dataSet1; private System.Data.DataTable dataTable1; private System.Data.DataColumn dataColumn1; private System.Data.DataColumn dataColumn2; private System.Data.DataColumn dataColumn3; private System.Data.DataColumn dataColumn4; private CustomGrid.MyGridView myGridView1; private DevExpress.XtraGrid.Columns.GridColumn colColumn1; private DevExpress.XtraGrid.Columns.GridColumn colColumn2; private DevExpress.XtraGrid.Columns.GridColumn colColumn3; private DevExpress.XtraGrid.Columns.GridColumn colColumn4; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.myGridControl1 = new CustomGrid.MyGridControl(); this.dataTable1 = new System.Data.DataTable(); this.dataColumn1 = new System.Data.DataColumn(); this.dataColumn2 = new System.Data.DataColumn(); this.dataColumn3 = new System.Data.DataColumn(); this.dataColumn4 = new System.Data.DataColumn(); this.myGridView1 = new CustomGrid.MyGridView(); this.colColumn1 = new DevExpress.XtraGrid.Columns.GridColumn(); this.colColumn2 = new DevExpress.XtraGrid.Columns.GridColumn(); this.colColumn3 = new DevExpress.XtraGrid.Columns.GridColumn(); this.colColumn4 = new DevExpress.XtraGrid.Columns.GridColumn(); this.dataSet1 = new System.Data.DataSet(); ((System.ComponentModel.ISupportInitialize)(this.myGridControl1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.myGridView1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit(); this.SuspendLayout(); // // myGridControl1 // this.myGridControl1.DataSource = this.dataTable1; this.myGridControl1.Dock = System.Windows.Forms.DockStyle.Fill; this.myGridControl1.Location = new System.Drawing.Point(0, 0); this.myGridControl1.MainView = this.myGridView1; this.myGridControl1.Name = "myGridControl1"; this.myGridControl1.Size = new System.Drawing.Size(536, 336); this.myGridControl1.TabIndex = 0; this.myGridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] { this.myGridView1}); // // dataTable1 // this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] { this.dataColumn1, this.dataColumn2, this.dataColumn3, this.dataColumn4}); this.dataTable1.TableName = "Table1"; // // dataColumn1 // this.dataColumn1.ColumnName = "Column1"; // // dataColumn2 // this.dataColumn2.ColumnName = "Column2"; // // dataColumn3 // this.dataColumn3.ColumnName = "Column3"; // // dataColumn4 // this.dataColumn4.ColumnName = "Column4"; // // myGridView1 // this.myGridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { this.colColumn1, this.colColumn2, this.colColumn3, this.colColumn4}); this.myGridView1.GridControl = this.myGridControl1; this.myGridView1.Name = "myGridView1"; this.myGridView1.OptionsView.ShowFooter = true; // // colColumn1 // this.colColumn1.Caption = "Column1"; this.colColumn1.FieldName = "Column1"; this.colColumn1.Name = "colColumn1"; this.colColumn1.Visible = true; this.colColumn1.VisibleIndex = 0; // // colColumn2 // this.colColumn2.Caption = "Column2"; this.colColumn2.FieldName = "Column2"; this.colColumn2.Name = "colColumn2"; this.colColumn2.Visible = true; this.colColumn2.VisibleIndex = 1; // // colColumn3 // this.colColumn3.Caption = "Column3"; this.colColumn3.FieldName = "Column3"; this.colColumn3.Name = "colColumn3"; this.colColumn3.Visible = true; this.colColumn3.VisibleIndex = 2; this.colColumn3.Width = 206; // // colColumn4 // this.colColumn4.Caption = "Column4"; this.colColumn4.FieldName = "Column4"; this.colColumn4.Name = "colColumn4"; this.colColumn4.Visible = true; this.colColumn4.VisibleIndex = 3; this.colColumn4.Width = 206; // // dataSet1 // this.dataSet1.DataSetName = "NewDataSet"; this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US"); this.dataSet1.Tables.AddRange(new System.Data.DataTable[] { this.dataTable1}); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(536, 336); this.Controls.Add(this.myGridControl1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.myGridControl1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.myGridView1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { dataTable1.Rows.Add(new object[] { 1, 2, 3, 4 }); } } }
MyBandedGridView/MyBandedGridPainter.cs(vb)
C#
using System; using System.Drawing; using DevExpress.XtraGrid.Drawing; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.BandedGrid; using DevExpress.XtraGrid.Views.Grid.Drawing; using DevExpress.XtraGrid.Views.BandedGrid.Drawing; using DevExpress.XtraGrid.Views.Grid.ViewInfo; namespace CustomGrid { public class MyBandedGridPainter : BandedGridPainter { public MyBandedGridPainter(BandedGridView view) : base(view) { } protected override void DrawRows(GridViewDrawArgs e) { base.DrawRows(e); DrawEmptyAreaLines(e); } protected virtual void DrawEmptyAreaLines(GridViewDrawArgs e) { GridViewRects rects = e.ViewInfo.ViewRects; Rectangle er = rects.EmptyRows; if(er.IsEmpty) return; Pen pen = SystemPens.ControlDark; if(View.OptionsView.ShowVerticalLines != DevExpress.Utils.DefaultBoolean.False) { foreach(GridColumnInfoArgs column in e.ViewInfo.ColumnsInfo) { int x = column.Bounds.Right - 1; if((column.Column != null && column.Column.Fixed != DevExpress.XtraGrid.Columns.FixedStyle.None) || ((rects.FixedLeft.IsEmpty || x > rects.FixedLeft.Right) && (rects.FixedRight.IsEmpty || x < rects.FixedRight.Left - 3))) e.Cache.DrawLine(pen, new Point(x, er.Top), new Point(x, er.Bottom)); } if(!rects.FixedRight.IsEmpty) e.Cache.DrawLine(pen, new Point(rects.FixedRight.Left - 1, er.Top), new Point(rects.FixedRight.Left - 1, er.Bottom)); } if(View.OptionsView.ShowHorizontalLines != DevExpress.Utils.DefaultBoolean.False) { int rowHeight = e.ViewInfo.MinRowHeight; for(int y = er.Top + rowHeight; y < er.Bottom; y += rowHeight) e.Cache.DrawLine(pen, new Point(er.Left, y), new Point(rects.DataRectRight.Right - 1, y)); } } } }
MyBandedGridView/MyBandedGridView.cs(vb)
C#
using System; namespace CustomGrid { public class MyBandedGridView : DevExpress.XtraGrid.Views.BandedGrid.BandedGridView { public MyBandedGridView() : this(null) {} public MyBandedGridView(DevExpress.XtraGrid.GridControl grid) : base(grid) {} protected override string ViewName { get { return "MyBandedGridView"; } } } }
MyGridControl.cs(vb)
C#
using System; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Registrator; namespace CustomGrid { public class MyGridControl : GridControl { protected override BaseView CreateDefaultView() { return CreateView("MyGridView"); } protected override void RegisterAvailableViewsCore(InfoCollection collection) { base.RegisterAvailableViewsCore(collection); collection.Add(new MyGridViewInfoRegistrator()); collection.Add(new MyBandedGridViewInfoRegistrator()); } } }
MyGridRegistration.cs(vb)
C#
using System; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Drawing; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Base.ViewInfo; using DevExpress.XtraGrid.Registrator; namespace CustomGrid { public class MyGridViewInfoRegistrator : GridInfoRegistrator { public override string ViewName { get { return "MyGridView"; } } public override BaseView CreateView(GridControl grid) { return new MyGridView(grid as GridControl); } public override BaseViewPainter CreatePainter(BaseView view) { return new MyGridPainter(view as MyGridView); } } public class MyBandedGridViewInfoRegistrator : BandedGridInfoRegistrator { public override string ViewName { get { return "MyBandedGridView"; } } public override BaseView CreateView(GridControl grid) { return new MyBandedGridView(grid as GridControl); } public override BaseViewPainter CreatePainter(BaseView view) { return new MyBandedGridPainter(view as MyBandedGridView); } } }
MyGridView/MyGridPainter.cs(vb)
C#
using System; using System.Drawing; using DevExpress.XtraGrid.Drawing; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.Drawing; using DevExpress.XtraGrid.Views.Grid.ViewInfo; namespace CustomGrid { public class MyGridPainter : GridPainter { public MyGridPainter(GridView view) : base(view) { } protected override void DrawRows(GridViewDrawArgs e) { base.DrawRows(e); DrawEmptyAreaLines(e); } protected virtual void DrawEmptyAreaLines(GridViewDrawArgs e) { GridViewRects rects = e.ViewInfo.ViewRects; Rectangle er = rects.EmptyRows; if(er.IsEmpty) return; Pen pen = SystemPens.ControlDark; if(View.OptionsView.ShowVerticalLines != DevExpress.Utils.DefaultBoolean.False) { foreach(GridColumnInfoArgs column in e.ViewInfo.ColumnsInfo) { int x = column.Bounds.Right - 1; if((column.Column != null && column.Column.Fixed != DevExpress.XtraGrid.Columns.FixedStyle.None) || ((rects.FixedLeft.IsEmpty || x > rects.FixedLeft.Right) && (rects.FixedRight.IsEmpty || x < rects.FixedRight.Left - 3))) e.Cache.DrawLine(pen, new Point(x, er.Top), new Point(x, er.Bottom)); } if(!rects.FixedRight.IsEmpty) e.Cache.DrawLine(pen, new Point(rects.FixedRight.Left - 1, er.Top), new Point(rects.FixedRight.Left - 1, er.Bottom)); } if(View.OptionsView.ShowHorizontalLines != DevExpress.Utils.DefaultBoolean.False) { int rowHeight = e.ViewInfo.MinRowHeight; for(int y = er.Top + rowHeight; y < er.Bottom; y += rowHeight) e.Cache.DrawLine(pen, new Point(er.Left, y), new Point(rects.DataRectRight.Right - 1, y)); } } } }
MyGridView/MyGridView.cs(vb)
C#
using System; namespace CustomGrid { public class MyGridView : DevExpress.XtraGrid.Views.Grid.GridView { public MyGridView() : this(null) {} public MyGridView(DevExpress.XtraGrid.GridControl grid) : base(grid) {} protected override string ViewName { get { return "MyGridView"; } } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.