在C#的列表视图的列中更改特定单元格中的值 [英] Change value in specific cell in column in list view in C#
问题描述
[C#中的列表视图] [1]
[List View in C# ][1]
仅在C#中,如何使它与值列中的更改值排成一排? [1]: https://i.stack.imgur.com/Wt2CM.png图片 或如何获取值"列中的特定单元格?
How to make it in one row with change values in value column only in C# ?? [1]: https://i.stack.imgur.com/Wt2CM.png image Or How to get specific cell in column Value?
我要添加一列,但列值会发生变化? 当应用程序运行时,列表视图不断添加新值,逐行添加行,我想添加ID,名称,类型和值的一行,以便以后选择.
I want to add one row with value change in column value? When app is run the list view keep adding row line by line with new value witch i want to add one row with ID , name , type and value to can selected later.
示例图片: 只有红色框中的值会更改
Example images: Only value in red box will change
[1]: https://i.stack.imgur.com/ofBE6.png [2]: https://i.stack.imgur.com/dismy.png [3] : https://i.stack.imgur.com/QH0Vj.png
[1]: https://i.stack.imgur.com/ofBE6.png [2]: https://i.stack.imgur.com/dismy.png [3]: https://i.stack.imgur.com/QH0Vj.png
代码
public partial class Form1 : Form
{
string r, rr, rrr;
private void timer2_Tick(object sender, EventArgs e)
{
ListViewItem lvi = new ListViewItem("1");
lvi.SubItems.Add(r);
lvi.SubItems.Add(rr);
lvi.SubItems.Add(rrr);
listView1.Items.Add(lvi);
}
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Computer cp = new Computer();
cp.Open();
cp.GPUEnabled = true;
cp.CPUEnabled = true;
cp.RAMEnabled = true;
cp.MainboardEnabled = true;
foreach (var hardware in cp.Hardware) // get Hardware in pc
{
if (hardware.HardwareType == HardwareType.RAM)
{
hardware.Update();
foreach (var senR in hardware.Sensors) //<== all sensor in ram
{
if (senR.SensorType == SensorType.Load) // <== 1 sensor RAM usage
{
Console.WriteLine("RAM: " + Convert.ToInt32(senR.Value) + " %");
r = senR.Name.ToString(); // sensor name
rr = senR.SensorType.ToString(); // sensor type
rrr = senR.Value.ToString(); // sensor value
}
}
}
}
}}
我可以使用列表视图的替代方法吗?
推荐答案
假设您的r是唯一值,则可以检查是否存在匹配的记录:
Assuming your r is the unique value, you can check to see if there's a matching record:
private void timer2_Tick(object sender, EventArgs e)
{
bool exists = false;
foreach (ListViewItem lvi in listView1.Items)
{
if ( lvi.SubItems(1).Text == r )
{
exists = true;
lvi.SubItems(2).Text = rr;
lvi.SubItems(3).Text = rrr;
}
}
if (!exists )
{
ListViewItem lvi = new ListViewItem("1");
lvi.SubItems.Add(r);
lvi.SubItems.Add(rr);
lvi.SubItems.Add(rrr);
listView1.Items.Add(lvi);
}
}
这是可行的,但说实话,我认为您应该更新一个DataTable和一个DataGridView. DataTable可以有一个唯一的列,您可以在源上进行检查查找,而不必在GUI上进行查找.
This is doable, but honestly, I think instead you should be updating a DataTable and a DataGridView. The DataTable can have a unique column and you can do a check find on the source, rather than doing a find on a GUI.
这篇关于在C#的列表视图的列中更改特定单元格中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!