当前位置:网站首页>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
边栏推荐
猜你喜欢

Optimization of five data submission methods

ECCV2022: Recursion on Transformer without adding parameters and less computation!

PHP序列化:eval

C#控件ListView用法

IDEA版Postman插件Restful Fast Request,细节到位,功能好用

PyQt5 rapid development and actual combat 10.1 Get city weather forecast

PyQt5快速开发与实战 9.7 UI层的自动化测试

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)

CentOS7 安装MySQL 图文详细教程

PHP Serialization: eval
随机推荐
Detailed explanation of network protocols and related technologies
/run/NetworkManager占用空间过大
IDEA版Postman插件Restful Fast Request,细节到位,功能好用
PyQt5快速开发与实战 9.7 UI层的自动化测试
SAP message TK 248 solved
报错:npm ERR code EPERM
selenium被反爬了怎么办?
Error: npm ERR code EPERM
计算机复试面试问题(计算机面试常见问题)
golang-gin-优雅重启
0X7FFFFFFF,0X80000000「建议收藏」
集群的安全模式
alert(1) (haozi.me)靶场练习
365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
PyQt5 rapid development and actual combat 10.2 compound interest calculation && 10.3 refresh blog clicks
NameNode (NN) 和SecondaryNameNode (2NN)工作机制
The importance of strategic offensive capability is much higher than strategic defensive capability
Flutter keyboard visibility
C# List用法 List介绍
PHP Serialization: eval