[dba-VB] WinForms: What is the best event for custom drawing on a leftmost cell of DataGridView head

Gustav Brock Gustav at cactus.dk
Wed Dec 1 03:04:44 CST 2010


Hi Shamil

Don't know about a DataGridView but for a tab control I used DrawItem. Nothing fancy:

<code>
        private void tabControlMail_DrawItem(object sender, DrawItemEventArgs e)
        {
            DrawTabColor(sender, e, Color.LightBlue, Color.Black, Color.BlueViolet, Color.Gray);
        }

        private void DrawTabColor(object sender, System.Windows.Forms.DrawItemEventArgs e, Color FocusedBackColor, Color FocusedForeColor, Color NonFocusedBackColor, Color NonFocusedForeColor)
        {
            Font myFont = new Font(e.Font, FontStyle.Regular);
            Brush backColorBrush;
            Brush foreColorBrush;
            StringFormat myFormat = new StringFormat();

            TabControl tabControl = (TabControl)sender;
            Rectangle rectangle = new Rectangle(tabControl.Left , 15,tabControl.Width, tabControl.Height);
            backColorBrush = new System.Drawing.SolidBrush(Color.Linen);
            foreColorBrush = new System.Drawing.SolidBrush(Color.Gray);
            e.Graphics.FillRectangle(backColorBrush, rectangle);
            
            Console.WriteLine("s: " + this.tabControlMail.SelectedIndex.ToString ()+ ", e: " + e.Index.ToString());

            if (e.Index == this.tabControlMail.SelectedIndex)
            {
                myFont = new Font(e.Font, FontStyle.Regular);
                backColorBrush = new System.Drawing.SolidBrush(FocusedBackColor);
                foreColorBrush = new System.Drawing.SolidBrush(FocusedForeColor);
                this.tabControlMail.TabPages[e.Index].BackColor = FocusedBackColor;
                string tabName = this.tabControlMail.TabPages[e.Index].Text;

                Rectangle rect = new Rectangle(e.Bounds.X + 4, e.Bounds.Y, e.Bounds.Width - 6, e.Bounds.Height);
                myFormat.Alignment = StringAlignment.Center;
                e.Graphics.FillRectangle(backColorBrush, rect);
                RectangleF r = new RectangleF(e.Bounds.X + 1, e.Bounds.Y + 4, e.Bounds.Width, e.Bounds.Height - 4);
                e.Graphics.DrawString(tabName, myFont, foreColorBrush, r, myFormat);
            }
            else
            {
                myFont = new Font(e.Font, FontStyle.Regular);
                backColorBrush = new System.Drawing.SolidBrush(NonFocusedBackColor);
                foreColorBrush = new System.Drawing.SolidBrush(NonFocusedForeColor);
                this.tabControlMail.TabPages[e.Index].BackColor = Color.Linen; // FocusedBackColor;
                string tabName = this.tabControlMail.TabPages[e.Index].Text;

                Rectangle rect = new Rectangle(e.Bounds.X + 1, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height + 1);
                myFormat.Alignment = StringAlignment.Center;
                e.Graphics.FillRectangle(backColorBrush, rect);
                RectangleF r = new RectangleF(e.Bounds.X, e.Bounds.Y + 4, e.Bounds.Width, e.Bounds.Height - 4);
                e.Graphics.DrawString(tabName, myFont, foreColorBrush, r, myFormat);
            }
            myFormat.Dispose();
            myFont.Dispose();
            backColorBrush.Dispose();
            foreColorBrush.Dispose();
        }
</code>

What are you trying to do?

/gustav


>>> shamil at smsconsulting.spb.ru 30-11-2010 19:44 >>>
Hi All --

What is the best suitable event for custom drawing on a top/left cell of
DataGridView ( .TopLeftHeaderCell)?

.RowHeadersVisible = true

Thank you.

--
Shamil
 

_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com 
http://databaseadvisors.com/mailman/listinfo/dba-vb 
http://www.databaseadvisors.com 






More information about the dba-VB mailing list