2 Replies - 5032 Views - Last Post: 21 August 2012 - 10:25 AM Rate Topic: -----

#1 Jelinios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-August 12

DataGridView ScrollBar Color

Posted 14 August 2012 - 10:11 AM

Hello,
I need to change the color of the DataGridView ScrollBars. I've made them visible all the time.
I've tried something with Controls[], but the color doesn't change...
I may later need to change the color of the column headers, font, cells etc.
If somebody know's how or know's a good webpage then please post me. Thank You

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ActiveXGrid
{
    partial class tabelenForm
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Komponenten-Designer generierter Code

        private void InitializeComponent()
        {
            this.tabele = new ActiveXGrid.CustomDataGrid();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.tabele)).BeginInit();
            this.SuspendLayout();
            // 
            // tabele
            // 
            this.tabele.BackgroundColor = System.Drawing.Color.LightBlue;
            this.tabele.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.tabele.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1});
            this.tabele.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabele.Location = new System.Drawing.Point(0, 0);
            this.tabele.Name = "tabele";
            this.tabele.RowHeadersVisible = false;
            this.tabele.Size = new System.Drawing.Size(491, 354);
            this.tabele.TabIndex = 0;
            this.tabele.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.tabele_CellDoubleClick);
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Column1";
            this.Column1.Name = "Column1";
            // 
            // tabelenForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.tabele);
            this.Name = "tabelenForm";
            this.Size = new System.Drawing.Size(491, 354);
            ((System.ComponentModel.ISupportInitialize)(this.tabele)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private CustomDataGrid tabele;
    }

    public class CustomDataGrid : DataGridView
    {
        public CustomDataGrid()
        {
            VerticalScrollBar.Visible = true;
            VerticalScrollBar.VisibleChanged += new EventHandler(VerScroll);
            HorizontalScrollBar.Visible = true;
            HorizontalScrollBar.VisibleChanged += new EventHandler(HorScroll);
        }

        private const int CAPTIONHEIGHT = 1;
        private const int CAPTIONWIDTH = 1;
        private const int BORDERWIDTH = 1;
              
        private void VerScroll(object sender, EventArgs e)
        {
            if (!VerticalScrollBar.Visible)
            {
                int width = VerticalScrollBar.Width;
                VerticalScrollBar.Location =
                  new Point(ClientRectangle.Width - width - BORDERWIDTH, CAPTIONHEIGHT);
                VerticalScrollBar.Size =
                  new Size(width, ClientRectangle.Height - CAPTIONHEIGHT - BORDERWIDTH - HorizontalScrollBar.Height);
                VerticalScrollBar.Show();
            }
        }

        private void HorScroll(object sender, EventArgs e)
        {
            if (!HorizontalScrollBar.Visible)
            {
                int height = HorizontalScrollBar.Height;
                HorizontalScrollBar.Location =
                  new Point(CAPTIONWIDTH, ClientRectangle.Height - height - BORDERWIDTH);
                HorizontalScrollBar.Size =
                  new Size(ClientRectangle.Width - CAPTIONWIDTH - BORDERWIDTH - VerticalScrollBar.Width, height);
                HorizontalScrollBar.Show();
            }
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: DataGridView ScrollBar Color

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,010
  • Joined: 05-May 12

Re: DataGridView ScrollBar Color

Posted 14 August 2012 - 10:40 AM

MSDN documents how to customize the painting of the column headers and cells: http://msdn.microsof...y/ms171618.aspx

As for overriding the way the scrollbars are painted. You may have to continue with route you've taken of subclassing the DataGridView. I was under the impression that if you are running Windows 7 with Aero GUI (as opposed to the classic Windows XP style) then you are stuck with the Aero rendering of the scrollbars. If you really need to, then you'll probably need to subclass the scrollbars themselves and do your custom drawing.
Was This Post Helpful? 0
  • +
  • -

#3 Jelinios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-August 12

Re: DataGridView ScrollBar Color

Posted 21 August 2012 - 10:25 AM

Thank You Skydiver for your reply. I left the scroll color to default. When I disabled the visual styles it was ok. The rest is easy to change in the Visual Studio, after little search ;) The topic can be closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1