DataGridView可以用来在WinForms中完成下面的表格UI吗? [英] Can DataGridView be used to accomplish the following table-like UI in WinForms?
问题描述
这是一个很痛苦的屁股,但我一直在使用一个 TableLayoutPanel
显示一些数据,如下面的屏幕截图所示。但有几次建议您可以轻松地使用 DataGridView
。
所以我的问题是,可以使用 DataGridView
在Windows窗体中完成以下屏幕截图吗?我搜索过网络,没有什么类似于以下内容。
答案将是是的,可以。或 不,不可能。。但请不要发布替代品,因为问题的目的是最终知道如果这样的事情可以实现一个 DataGridView
。
DataTable
如下所示:
BG_Color EmpId ColNum RowNum
黄色4304 1 1
黄色8464 2 1
黄色2012 3 1
蓝色4593 1 2
蓝色3515 2 2
蓝色0546 3 2
绿色4346 1 3
绿色5426 2 3
绿色0551 3 3
结果如下。它是只显示,在这里没有任何可点击
如何使用 DataGridView
可以实现?我甚至包括每个单元格的位置,通过 RowNum
和 ColNum
,以防万一需要。 / p>
或者如果有人可以在类似的东西上分享链接,那将不胜感激。
谢谢。 >
是的,您可以使用 DataGridView
创建此类用户界面:
- 您可以设置
RowCount
和ColumnCount
c code code code code code code code code code $ c $设置单元格的值和背面颜色。 - 您不能使用数据绑定与当前数据结构。
以下是您的示例数据的工作示例:
DataTable表;
private void Form1_Load(object sender,EventArgs e)
{
table = GetData();
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
this.dataGridView1.RowTemplate.Height = 64;
this.dataGridView1.CellFormatting + = dataGridView1_CellFormatting;
this.dataGridView1.ColumnCount =(int)table.Compute(Max(ColNum),);
this.dataGridView1.RowCount =(int)table.Compute(Max(RowNum),);
}
void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if(e.RowIndex> = 0& e.ColumnIndex> = 0)
{
var row = table.Select(string.Format(RowNum = {0} AND ColNum = {1},
e.RowIndex + 1,e.ColumnIndex + 1))。FirstOrDefault );
if(row!= null)
{
e.Value = row [EmpId];
var color =(Color)new ColorConverter()。ConvertFrom(row [BG_Color]);
e.CellStyle.BackColor = color;
e.CellStyle.SelectionBackColor = color;
e.CellStyle.SelectionForeColor = Color.Black;
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
}
}
It's been a pain in the butt, but I've been using a TableLayoutPanel
to display some data as seen in the below screenshot. But several times it's been suggested that this can be easily done with a DataGridView
.
So my question is, can the following screenshot be accomplished in a Windows Form using a DataGridView
? I've searched the web and nothing similar to what's below.
An answer would be "Yes, it can." or "No, it's not possible.". But please do not post alternatives since the purpose of the question is to finally know if something like this can be accomplished a DataGridView
.
The DataTable
looks like the following:
BG_Color EmpId ColNum RowNum
Yellow 4304 1 1
Yellow 8464 2 1
Yellow 2012 3 1
Blue 4593 1 2
Blue 3515 2 2
Blue 0546 3 2
Green 4346 1 3
Green 5426 2 3
Green 0551 3 3
The result is the following. It's display-only and nothing's clickable here:
How is this possible with a DataGridView
? I've even included where exactly each cell will be, by RowNum
and ColNum
, just in case it's needed.
Or if someone can share a link on something similar, it would be appreciated.
Thanks.
Yes, you can create such user interface using a DataGridView
:
- You can set
RowCount
andColumnCount
of grid manually. - You can hanlde
CellFormatting
event ofDataGridView
and set the value and back color of cell there. - You can not use data-binding with current structure of data.
Here is a working example with your sample data:
DataTable table;
private void Form1_Load(object sender, EventArgs e)
{
table = GetData();
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
this.dataGridView1.RowTemplate.Height = 64;
this.dataGridView1.CellFormatting += dataGridView1_CellFormatting;
this.dataGridView1.ColumnCount = (int)table.Compute("Max(ColNum)", "");
this.dataGridView1.RowCount = (int)table.Compute("Max(RowNum)", "");
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex >= 0 & e.ColumnIndex >= 0)
{
var row = table.Select(string.Format("RowNum={0} AND ColNum={1}",
e.RowIndex + 1, e.ColumnIndex + 1)).FirstOrDefault();
if (row != null)
{
e.Value = row["EmpId"];
var color = (Color)new ColorConverter().ConvertFrom(row["BG_Color"]);
e.CellStyle.BackColor = color;
e.CellStyle.SelectionBackColor = color;
e.CellStyle.SelectionForeColor = Color.Black;
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
}
}
这篇关于DataGridView可以用来在WinForms中完成下面的表格UI吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!