[英]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.