簡體   English   中英

使用 Tablelayoutpanel 為 DGW 創建 header 行

[英]Creating header row for DGW with Tablelayoutpanel

我有一個表格布局面板,下面是一個有 9 列的數據網格視圖。 我想要實現的是在 tablelayoutpanel 中創建 7 個單元格(或列)。 我希望 tablelayoutpanel 中的第一個單元格(或列)跨越 datagridview 的前三列,然后接下來的 6 列(在 tablelayoutpanel 中)與 datagridview 中的其他 6 列對齊。 下面是一張展示最終結果的圖片:

在此處輸入圖像描述

到目前為止,這是我的代碼。 正如你所看到的,我還沒有走多遠。 我只是想知道這是否容易做到? 如果是這樣,有人可以編輯我的代碼以使其正常工作嗎? 考慮到 window 可以調整大小,這是否可能? 該項目是在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;
    }

這些數字是近似的,可以讓您了解如何執行此操作

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++;
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM