简体   繁体   中英

Creating header row for DGW with Tablelayoutpanel

I have a tablelayoutpanel and below that is a datagridview with 9 columns. What I want to achieve is to create 7 cells (or columns) in the tablelayoutpanel. I want the first cell (or column) in the tablelayoutpanel to span the first three columns of the datagridview and then the next 6 columns (in the tablelayoutpanel) to be aligned with the other 6 columns in the datagridview. Beneath is a picture to demonstrate the end result:

在此处输入图像描述

Here is my code so far. As you can see I havent very gotten far. I am just wondering if this easily done? If so can someone edit my code to get it working? Is this even possible considering the window will be resizable? The project is created in winforms.

    public void SetDummyData()
    {
        var data = GetDummyData();
        var binding = new BindingSource { DataSource = data };           
        dataGridView1.DataSource = binding;
    }

    public List<TestData> GetDummyData()
    {
        return new List<TestData>()
        {
            new TestData { D1 = "", D2 = "", D3 = "", D4 = "", D5 = "", D6 = "", D7 = "", D8 = "", D9 = "" },
            new TestData { D1 = "", D2 = "", D3 = "", D4 = "", D5 = "", D6 = "", D7 = "", D8 = "", D9 = "" },
            new TestData { D1 = "", D2 = "", D3 = "", D4 = "", D5 = "", D6 = "", D7 = "", D8 = "", D9 = "" },
            new TestData { D1 = "", D2 = "", D3 = "", D4 = "", D5 = "", D6 = "", D7 = "", D8 = "", D9 = "" },
        };
    }

    public void SetTableLayout()
    {
        EmptyTable();
        tableLayoutPanel1.ColumnCount = 7;
        tableLayoutPanel1.RowCount = 1;
        foreach (DataGridViewColumn c in dataGridView1.Columns)
        {
            var percentage = GetDGWColumnPercentage(c);
            // Cant add percentage here?
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(percentage));
            // Then add label
        }
    }

    public void EmptyTable()
    {
        for (int i = tableLayoutPanel1.Controls.Count - 1; i >= 0; --i)
            tableLayoutPanel1.Controls[i].Dispose();
        tableLayoutPanel1.Controls.Clear();
        tableLayoutPanel1.RowCount = 0;
    }

    public double GetDGWColumnPercentage(DataGridViewColumn c)
    {
        return (c.Width * 100) / dataGridView1.Width;
    }

The numbers are approximate to give you an idea of how to do this

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

namespace WindowsFormsApplication71
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Panel panel1 = new Panel();
            panel1.Top = 100;
            panel1.Left = 100;
            panel1.Width = 600;
            panel1.Height = 100;
            panel1.BackColor = Color.Blue;
            this.Controls.Add(panel1);
            Panel panel2 = new Panel();
            panel2.Top = 10;
            panel2.Left = 20;
            panel2.Width = 180;
            panel2.Height = 80;
            panel2.BackColor = Color.Red;
            panel1.Controls.Add(panel2);
            Panel panel3 = new Panel();
            panel3.Top = 10;
            panel3.Left = 220;
            panel3.Width = 360;
            panel3.Height = 80;
            panel3.BackColor = Color.Yellow;
            panel1.Controls.Add(panel3);

            DataGridView dgv1 = new DataGridView();
            dgv1.Left = 100;
            dgv1.Top = 200;
            dgv1.Width = 600;
            dgv1.Height = 300;
            this.Controls.Add(dgv1);

            DataTable dt = new DataTable();
            dt.Columns.Add("Col 1", typeof(string));
            dt.Columns.Add("Col 2", typeof(string));
            dt.Columns.Add("Col 3", typeof(string));
            dt.Columns.Add("Col 4", typeof(string));
            dt.Columns.Add("Col 5", typeof(string));
            dt.Columns.Add("Col 6", typeof(string));
            dt.Columns.Add("Col 7", typeof(string));
            dt.Columns.Add("Col 8", typeof(string));
            dt.Columns.Add("Col 9", typeof(string));



            dgv1.DataSource = dt;

            int count = 0;
            foreach (DataGridViewColumn col in dgv1.Columns)
            {
                if (count < 3)
                {
                    col.Width = 60;
                }
                else
                {
                    col.Width = 40;
                }
                count++;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM