C#DataGridView科尔斯潘

编程入门 行业动态 更新时间:2024-10-15 12:35:28
本文介绍了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科尔斯潘

本文发布于:2023-11-10 16:24:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:科尔   DataGridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!