none
请问如何用DataGridView做出这样的效果 RRS feed

答案

  • 你好,

    你需要自己去画Column header去实现这个需求:

    Namespace Sample4
    {
        public partial class DgvColumnHeaderMerge : Form
        {
            Public DgvColumnHeaderMerge()
            {
                InitializeComponent();
            }
            private void DgvColumnHeaderMerge_Load(object sender, EventArgs e)
            {
                this.dataGridView1.Columns.Add("JanWin", "Win");
                this.dataGridView1.Columns.Add("JanLoss", "Loss");
                this.dataGridView1.Columns.Add("FebWin", "Win");
                this.dataGridView1.Columns.Add("FebLoss", "Loss");
                this.dataGridView1.Columns.Add("MarWin", "Win");
                this.dataGridView1.Columns.Add("MarLoss", "Loss");
                for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
                {
                    this.dataGridView1.Columns[j].Width = 45;
                }
                this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                this.dataGridView1.ColumnHeadersHeight = this.dataGridView1.ColumnHeadersHeight * 2;
                this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
                this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
                this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
            }
            void dataGridView1_Paint(object sender, PaintEventArgs e)
            {
                string[] monthes = { "January", "February", "March" };
                for (int j = 0; j < 6;)
                {
                    Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true); //get the column header cell
                    r1.X += 1;
                    r1.Y += 1;
                    r1.Width = r1.Width * 2 - 2;
                    r1.Height = r1.Height / 2 - 2;
                    e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
                    StringFormat format = new StringFormat();
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;
                    e.Graphics.DrawString(monthes[j/2],
                        this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                        new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                        r1,
                        format);
                    j += 2;
                }
            }
            void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.RowIndex == -1 && e.ColumnIndex > -1)
                {
                    e.PaintBackground(e.CellBounds, false);
                    Rectangle r2 = e.CellBounds;
                    r2.Y += e.CellBounds.Height / 2;
                    r2.Height = e.CellBounds.Height / 2;
                    e.PaintContent(r2);
                    e.Handled = true;
                }
            }
        }
    }
    
    我参考了这个帖子:
    http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/531577e8-0be3-406d-a81b-48f8ed02e8df/
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年6月30日 3:51
    版主

全部回复

  • 这种事dataGrid实现主从查询吧。下面是从表数据信息,最上面是主表对应信息。,关键是你得定义数据集中主表与从表的关联,,试试DataRelation 对象,请高手指正
    ♡. Microsoft .NET Platform
    2009年6月28日 10:42
  • 你好!
      如果你使用比较老的控件DataGrid(DataGridView的上一代)可以直接实现!
         如果使用DataGridView可以使用两个来显示!
    周雪峰
    2009年6月28日 11:00
    版主
  • 你好.. 非常感激大家的帮忙 虽然我现在不懂,,尽力去查查
    2009年6月28日 11:41
  • 你好,

    你需要自己去画Column header去实现这个需求:

    Namespace Sample4
    {
        public partial class DgvColumnHeaderMerge : Form
        {
            Public DgvColumnHeaderMerge()
            {
                InitializeComponent();
            }
            private void DgvColumnHeaderMerge_Load(object sender, EventArgs e)
            {
                this.dataGridView1.Columns.Add("JanWin", "Win");
                this.dataGridView1.Columns.Add("JanLoss", "Loss");
                this.dataGridView1.Columns.Add("FebWin", "Win");
                this.dataGridView1.Columns.Add("FebLoss", "Loss");
                this.dataGridView1.Columns.Add("MarWin", "Win");
                this.dataGridView1.Columns.Add("MarLoss", "Loss");
                for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
                {
                    this.dataGridView1.Columns[j].Width = 45;
                }
                this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                this.dataGridView1.ColumnHeadersHeight = this.dataGridView1.ColumnHeadersHeight * 2;
                this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
                this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
                this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
            }
            void dataGridView1_Paint(object sender, PaintEventArgs e)
            {
                string[] monthes = { "January", "February", "March" };
                for (int j = 0; j < 6;)
                {
                    Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true); //get the column header cell
                    r1.X += 1;
                    r1.Y += 1;
                    r1.Width = r1.Width * 2 - 2;
                    r1.Height = r1.Height / 2 - 2;
                    e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
                    StringFormat format = new StringFormat();
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;
                    e.Graphics.DrawString(monthes[j/2],
                        this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                        new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                        r1,
                        format);
                    j += 2;
                }
            }
            void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.RowIndex == -1 && e.ColumnIndex > -1)
                {
                    e.PaintBackground(e.CellBounds, false);
                    Rectangle r2 = e.CellBounds;
                    r2.Y += e.CellBounds.Height / 2;
                    r2.Height = e.CellBounds.Height / 2;
                    e.PaintContent(r2);
                    e.Handled = true;
                }
            }
        }
    }
    
    我参考了这个帖子:
    http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/531577e8-0be3-406d-a81b-48f8ed02e8df/
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年6月30日 3:51
    版主