C#DataGridView科尔斯潘 [英] C# DataGridView Colspan

查看:87
本文介绍了C#DataGridView科尔斯潘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 datagridview 动态创建一个类似excel的表,但是我想选择设置 colspan 到某些列。

I want to dynamically create a excel-like table with datagridview, but I want to have the option to set colspan to some columns.

此想法只是为了显示数据,用户不会键入任何内容,但应以表格/电子表格的形式显示。如果我不能在 colspan 中使用 datagridview ,是否有其他类型的表型工具具有 colspan

The idea is just to display data, the user will not type anything, but it should be in table/spreadsheet look. If I cannot have datagridview with colspan is there any other type of table-like tool which has a colspan?

另一方面,将从数据库查询结果中动态创建列。

I other hand the columns will be created dynamically from database query result.

我正在使用Windows窗体。

I'm using windows forms.

有任何想法吗?

推荐答案

您可以看看 TableLayoutPanel类,其中具有 TableLayoutPanel.SetColumnSpan方法

You might take a look at the TableLayoutPanel Class, which has a TableLayoutPanel.SetColumnSpan Method.

这是一个代码示例,它跨越2列中的一个文本框:

Here's a code sample, which spans the one of the text boxes on 2 columns:

var dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Value1");
dt.Columns.Add("Value2");

dt.Rows.Add(1, "aa", "xx");
dt.Rows.Add(2, "bb","yy");
dt.Rows.Add(3, "cc", "zz");
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.ColumnCount = 4;
tableLayoutPanel1.AutoSize = true;
for (int i = 0; i < dt.Columns.Count; i++)
{
    var l = new Label();
    l.Dock = DockStyle.Fill;
    l.Text = dt.Columns[i].ColumnName;
    tableLayoutPanel1.Controls.Add(l, i, 0);
}
var emptyLabel = new Label();
emptyLabel.Text = "Empty label";
tableLayoutPanel1.Controls.Add(emptyLabel, 4, 0);
for (int i = 0; i < dt.Rows.Count; i++)
{
    for (int j = 0; j < dt.Rows[i].ItemArray.Length; j++)
    {
        var tb = new TextBox();
        tb.Multiline = true;
        tb.Dock = DockStyle.Fill;
        tb.Text = dt.Rows[i][j].ToString();
        tableLayoutPanel1.Controls.Add(tb, j, i+1);
        if (i == 1 && j == 2)
            tableLayoutPanel1.SetColumnSpan(tb, 2);
    }
}

这篇关于C#DataGridView科尔斯潘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆