More Group Sites
School Rankings
Jobless Net
Better Home
Enviro++


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest Search | Active Topics | Members | Log In | Register

C# - dynamic vertical column headings in DataGridView Options · View
hong
Posted: Monday, July 12, 2010 6:25:11 PM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 335
Points: 711
Location: Australia
The following code shows how to dynamically insert columns into DataGridView based on the data stored in a database. Since the datagridview can be crowded, it is required to make some column headings vertical. To do this, we need to enable resizing of the column header height and do the transformation in the event handler CellPainting.
Code:
        private void Form1_Load(object sender, EventArgs e)
        {
            string strSql = "<your SELECT statement>";
            //get the column headings
        
        ArrayList columnHeadings = ...

            if (columnHeadings.Count > 0)
            {
                dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                dataGridView1.ColumnHeadersHeight = 100;
            }
            else
            {
                dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            }
            for (int i = 0; i < columnHeadings.Count; i++)
            {
                DataGridViewColumn column = new DataGridViewCheckBoxColumn();
                dataGridView1.Columns.Insert(dataGridViewCheckBoxColumn1.Index + 1, column);
                column.HeaderText = columnHeadings[i].ToString();
                column.Width = 20;
            }
        }


Code:
        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && (e.ColumnIndex > dataGridViewCheckBoxColumn1.Index) && (e.ColumnIndex < dataGridViewCheckBoxColumn2.Index))
            {
                e.PaintBackground(e.CellBounds, true);
                e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom);
                e.Graphics.RotateTransform(270);
                e.Graphics.DrawString(e.FormattedValue.ToString(), e.CellStyle.Font, Brushes.Black, 5, 5);
                e.Graphics.ResetTransform();
                e.Handled = true;

            }
        }

Sponsor
Posted: Monday, July 12, 2010 6:25:11 PM
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.228 seconds.