本文介绍了如何在datagridview中添加子列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在datagridview中添加子列。

How to Add Sub Column in datagridview .

推荐答案

int flag=0;







onForm loading活动






onForm load Event

#region TestCode
           try
           {
               this.dgvCreateResult.Columns.Add("1", "");
               this.dgvCreateResult.Columns.Add("2", "");
               this.dgvCreateResult.Columns.Add("3", "");
               this.dgvCreateResult.Columns.Add("4", "");

               this.dgvCreateResult.Columns.Add("Eng1", "FA1");

               this.dgvCreateResult.Columns.Add("Eng2", "SA1");

               this.dgvCreateResult.Columns.Add("Math1", "FA1");

               this.dgvCreateResult.Columns.Add("Math2", "SA1");

               this.dgvCreateResult.Columns.Add("Hin1", "FA1");

               this.dgvCreateResult.Columns.Add("Hin2", "SA1");

               this.dgvCreateResult.Columns.Add("Phy1", "FA1");

               this.dgvCreateResult.Columns.Add("Phy2", "SA1");

               this.dgvCreateResult.Columns.Add("Che1", "FA1");

               this.dgvCreateResult.Columns.Add("Che2", "SA1");

               this.dgvCreateResult.Columns.Add("colTotal", "");

               for (int j = 0; j < this.dgvCreateResult.ColumnCount; j++)
               {
                   if (j >= 0 && j < 4)
                   {
                       if (j == 0)
                       {
                           dgvCreateResult.Columns[j].Width = 50;
                           dgvCreateResult.Columns[j].Frozen = true;
                       }
                       else
                       {
                           dgvCreateResult.Columns[j].Width = 120;
                           dgvCreateResult.Columns[j].Frozen = true;
                       }
                   }
                   else
                   {
                       if (j == dgvCreateResult.Columns.Count - 1)
                       {
                           dgvCreateResult.Columns[j].Width = 100;
                       }
                       else
                       {
                           dgvCreateResult.Columns[j].Width = 50;
                       }
                   }
                   dgvCreateResult.Columns[j].SortMode = DataGridViewColumnSortMode.NotSortable;
               }

               this.dgvCreateResult.ColumnHeadersHeightSizeMode =

                    DataGridViewColumnHeadersHeightSizeMode.EnableResizing;

               this.dgvCreateResult.ColumnHeadersHeight =

                           this.dgvCreateResult.ColumnHeadersHeight * 2;

               this.dgvCreateResult.ColumnHeadersDefaultCellStyle.Alignment =

                    DataGridViewContentAlignment.BottomCenter;

               this.dgvCreateResult.CellPainting += new

                    DataGridViewCellPaintingEventHandler(dgvCreateResult_CellPainting);

               this.dgvCreateResult.Paint += new PaintEventHandler(dgvCreateResult_Paint);
           }
           catch (Exception Ex) { }
           #endregion














private void dgvCreateResult_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;

                flag = 0;

            }
        }










private void dgvCreateResult_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                string[] monthes = { "SNo", "AdmissionNo", "Name", "Grade/Percentage", "English", "Math", "Hindi","Physics","Chemistry","Total" };

                for (int j = 0; j < dgvCreateResult.Columns.Count; )
                {

                    //get the column header cell

                    Rectangle r1 = this.dgvCreateResult.GetCellDisplayRectangle(j, -1, true);



                    r1.X += 1;

                    r1.Y += 1;

                    int a = j;

                    if (j >= 0 && j < 4)
                    {
                        r1.Width = r1.Width - 2;

                    }
                    else if (j ==dgvCreateResult.Columns.Count-1)
                    {
                        r1.Width = r1.Width - 2;
                        a = j - flag;
                        flag++;
                    }
                    else
                    {
                        r1.Width = r1.Width * 2 - 2;
                        a = j - flag;
                        flag++;
                    }

                    r1.Height = r1.Height / 2 - 2;

                    e.Graphics.FillRectangle(new SolidBrush(dgvCreateResult.ColumnHeadersDefaultCellStyle.BackColor), r1);

                    StringFormat format = new StringFormat();

                    format.Alignment = StringAlignment.Center;

                    format.LineAlignment = StringAlignment.Center;

                    e.Graphics.DrawString(monthes[a],

                    this.dgvCreateResult.ColumnHeadersDefaultCellStyle.Font,

                    new SolidBrush(this.dgvCreateResult.ColumnHeadersDefaultCellStyle.ForeColor),

                    r1,

                    format);

                    if (j >= 0 && j < 4)
                    {
                        j++;
                    }
                    else if (j == dgvCreateResult.Columns.Count - 2)
                    {
                        j++;
                    }
                    else
                    {
                        j += 2;
                    }

                }
            }
            catch (Exception Ex) { }
        }










private void dgvCreateResult_Scroll(object sender, ScrollEventArgs e)
        {
            dgvCreateResult.Refresh();

        }


这篇关于如何在datagridview中添加子列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:32