当前位置:网站首页>C# control ListView usage
C# control ListView usage
2022-07-31 13:16:00 【dusk and starry sky】
Add title
Show grid
Add data to the table
Customize other properties
End
Add Title
Add a ListView component in the Winfrom interface, then click the arrow in the upper right corner, click Edit Column
Add the title below, then click OK
At this time, the ListView is still blank, and these titles cannot be displayed. Select Details in the view here
It will be as shown in the figure below, although the title is out, the content is indeed a white version, I still think the DataGridView component is easy to use at this time
Show grid
At this point, the table is just blank, you canSet in the properties panel to display the grid, as shown below
At this time, as shown in the figure below, the effect comes out, but there are still shortcomings in it
Add data to the table
What should I do if no data is added?Copied directly from the Internet, as follows:
private void Button_Test_Click(object sender, EventArgs e)
{
//Data update, UI is temporarily suspended until EndUpdate draws controls, which can effectively avoid flickering and greatly improve loading speed
this.listView1.BeginUpdate();
//Add 5 rows of data
for (int i = 0; i < 5; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add("column 1,th" + i + "row");
lvi.SubItems.Add("column 2, th" + i + "row");
lvi.SubItems.Add("column 3, th" + i + "row");
lvi.SubItems.Add("4th column, "+i + "row");
lvi.SubItems.Add("5th column," + i + "row");
this.listView1.Items.Add(lvi);
}
//End data processing, the UI interface is drawn at one time.
this.listView1.EndUpdate();
}
After running, let's see the effect, what? There is no data in the first line serial number,
Check the breakpoint, the ListViewItem array index 0 is actually empty
I searched for information, but I still found a solution:
private void Button_Test_Click(object sender, EventArgs e)
{
//Data update, UI is temporarily suspended until EndUpdate draws controls, which can effectively avoid flickering and greatly improve loading speed
listView1.BeginUpdate();
//Add 5row data
for (int i = 0; i < 5; i++)
{
int column = i + 1;
ListViewItem lvi = listView1.Items.Add("column 1,th" + column + "row");
lvi.SubItems.Add("column 2," + column + "row");
lvi.SubItems.Add("column 3," + column + "row");
lvi.SubItems.Add("Column 4," + column + "row");
lvi.SubItems.Add("Column 5," + column + "row");
}
//EndData processing, the UI interface is drawn at one time.
listView1.EndUpdate();
}
This kind of writing looks very painful, and there is no other way for the time being
Now the effect is achieved, but what if I want to change the properties of one of the cells?
Customize other properties
Example 1: Change the background color and font color
private void Button_Test_Click(object sender, EventArgs e)
{
//Data update, UI is temporarily suspended until EndUpdate draws controls, which can effectively avoid flickering and greatly improve loading speed
listView1.BeginUpdate();
//Add 5row data
for (int i = 0; i < 5; i++)
{
int column = i + 1;
ListViewItem lvi = listView1.Items.Add("column 1,th" + column + "row");
lvi.SubItems.Add("column 2," + column + "row");
lvi.SubItems.Add("column 3," + column + "row");
lvi.SubItems.Add("column 4," + column + "row");
lvi.SubItems.Add("column 5," + column + "row");
if (column % 2 ==0)
{
lvi.BackColor = Color.Red;
lvi.ForeColor = Color.White;
}
else
{
lvi.BackColor = Color.Green;
lvi.ForeColor = Color.Black;
}
}
//End data processing, UI interface is one-timedraw.
listView1.EndUpdate();
}
Effect:
Other attributes will not be demonstrated, and the writing is similar.
End
边栏推荐
猜你喜欢
随机推荐
JSP中如何借助response对象实现页面跳转呢?
知名无人驾驶公司:文远知行内推
计算机复试面试问题(计算机面试常见问题)
Selenium自动化测试之Selenium IDE
365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
硬盘分区,拓展C盘,不重装系统,不重装D盘软件的全教程。
攻防演练丨赛宁红方管控平台走进广东三地 助力数字政府网络安全建设
AMBA APB学习记录(AMBA 3/4)
IDEA如何运行web程序
Network layer key protocol - IP protocol
如何使用StarUML画类图[通俗易懂]
Six Stones Programming: No matter which function you think is useless, people who can use it will not be able to leave, so at least 99%
分布式监视 Zabbix 和 Prometheus 到底怎么选?千万别用错了!
图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
架构实战营|模块8
The operator,
电商rpa是什么意思?跟电商rpi是一个意思吗?
六石编程学:不论是哪个功能,你觉得再没用,会用的人都离不了,所以至少要做到99%
基于神经网络的多柔性梁耦合结构振动控制
golang八股文整理(持续搬运)