C# 如何将 <list>string 添加到 Listview
C# How to add <list>string into Listview
我有 List<string>
,其中包含 cmd 中 netstat 命令的解析输出,您看到的列表是这样的:
TCP
0.0.0.0:135
0.0.0.0:0
LISTENING
1028
TCP
0.0.0.0:445
0.0.0.0:0
LISTENING
4
TCP
0.0.0.0:5040
0.0.0.0:0
LISTENING
1328
TCP
0.0.0.0:5357
0.0.0.0:0
LISTENING
4
我有一个包含 4 列的列表视图,如下所示:
我想将每 5 行添加到列表视图中的一行中,以便它们与列协议匹配,等等我该如何实现?
如果您始终获得相同数量的列,那么您可以使用以下代码。
就像,你总是得到 5 列。
TCP
0.0.0.0:135
0.0.0.0:0
正在聆听
1028
import React from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { Table } from 'antd';
const { Column } = Table;
const data = [
'TCP',
'0.0.0.0:135',
'0.0.0.0:0',
'LISTENING',
'1028',
'TCP',
'0.0.0.0:445',
'0.0.0.0:0',
'LISTENING',
'4',
'TCP',
'0.0.0.0:5040',
'0.0.0.0:0',
'LISTENING',
'1328',
'TCP',
'0.0.0.0:5357',
'0.0.0.0:0',
'LISTENING',
'4'
];
let newData = [];
let counter = 0;
let index = 0;
Object.keys(data).map(function(key) {
if (counter == 0) {
newData.splice(index, 0, { ...newData[index], Protocol: data[key] });
counter = 1;
} else if (counter == 1) {
newData.splice(index, 0, { ...newData[index], Address1: data[key] });
newData.splice(index+1, 1);
counter = 2;
} else if (counter == 2) {
newData.splice(index, 0, { ...newData[index], Address2: data[key] });
newData.splice(index+1, 1);
counter = 3;
} else if (counter == 3) {
newData.splice(index, 0, { ...newData[index], Etat: data[key] });
newData.splice(index+1, 1);
counter = 4;
} else if (counter == 4) {
newData.splice(index, 0, { ...newData[index], ProcNa: data[key] });
newData.splice(index+1, 1);
counter = 0;
index = index + 1;
}
});
ReactDOM.render(
<Table dataSource={newData}>
<Column title="Protocol" dataIndex="Protocol" key="Protocol" />
<Column title="Address1" dataIndex="Address1" key="Address1" />
<Column title="Address2" dataIndex="Address2" key="Address2" />
<Column title="Etat" dataIndex="Etat" key="Etat" />
<Column title="ProcNa" dataIndex="ProcNa" key="ProcNa" />
</Table>,
document.getElementById('container')
);
// Cheack to see if the number of items in the list in divisible by 5
if (validElements.Count % 5 != 0)
{
//keep adding items until the reminder is 0
while (validElements.Count % 5 != 0) {
validElements.Add("na");
}
}
// adding the items to the listview
for (var i = 0; i < validElements.Count; i +=5)
{
ListViewItem lvi = new ListViewItem(validElements[i]);
lvi.SubItems.Add(validElements[i+1]);
lvi.SubItems.Add(validElements[i+2]);
lvi.SubItems.Add(validElements[i+3]);
lvi.SubItems.Add(validElements[i+4]);
listView1.Items.Add(lvi);
}
我有 List<string>
,其中包含 cmd 中 netstat 命令的解析输出,您看到的列表是这样的:
TCP
0.0.0.0:135
0.0.0.0:0
LISTENING
1028
TCP
0.0.0.0:445
0.0.0.0:0
LISTENING
4
TCP
0.0.0.0:5040
0.0.0.0:0
LISTENING
1328
TCP
0.0.0.0:5357
0.0.0.0:0
LISTENING
4
我有一个包含 4 列的列表视图,如下所示:
我想将每 5 行添加到列表视图中的一行中,以便它们与列协议匹配,等等我该如何实现?
如果您始终获得相同数量的列,那么您可以使用以下代码。
就像,你总是得到 5 列。
TCP
0.0.0.0:135
0.0.0.0:0
正在聆听
1028
import React from 'react'; import ReactDOM from 'react-dom'; import 'antd/dist/antd.css'; import './index.css'; import { Table } from 'antd'; const { Column } = Table; const data = [ 'TCP', '0.0.0.0:135', '0.0.0.0:0', 'LISTENING', '1028', 'TCP', '0.0.0.0:445', '0.0.0.0:0', 'LISTENING', '4', 'TCP', '0.0.0.0:5040', '0.0.0.0:0', 'LISTENING', '1328', 'TCP', '0.0.0.0:5357', '0.0.0.0:0', 'LISTENING', '4' ]; let newData = []; let counter = 0; let index = 0; Object.keys(data).map(function(key) { if (counter == 0) { newData.splice(index, 0, { ...newData[index], Protocol: data[key] }); counter = 1; } else if (counter == 1) { newData.splice(index, 0, { ...newData[index], Address1: data[key] }); newData.splice(index+1, 1); counter = 2; } else if (counter == 2) { newData.splice(index, 0, { ...newData[index], Address2: data[key] }); newData.splice(index+1, 1); counter = 3; } else if (counter == 3) { newData.splice(index, 0, { ...newData[index], Etat: data[key] }); newData.splice(index+1, 1); counter = 4; } else if (counter == 4) { newData.splice(index, 0, { ...newData[index], ProcNa: data[key] }); newData.splice(index+1, 1); counter = 0; index = index + 1; } }); ReactDOM.render( <Table dataSource={newData}> <Column title="Protocol" dataIndex="Protocol" key="Protocol" /> <Column title="Address1" dataIndex="Address1" key="Address1" /> <Column title="Address2" dataIndex="Address2" key="Address2" /> <Column title="Etat" dataIndex="Etat" key="Etat" /> <Column title="ProcNa" dataIndex="ProcNa" key="ProcNa" /> </Table>, document.getElementById('container') );
// Cheack to see if the number of items in the list in divisible by 5
if (validElements.Count % 5 != 0)
{
//keep adding items until the reminder is 0
while (validElements.Count % 5 != 0) {
validElements.Add("na");
}
}
// adding the items to the listview
for (var i = 0; i < validElements.Count; i +=5)
{
ListViewItem lvi = new ListViewItem(validElements[i]);
lvi.SubItems.Add(validElements[i+1]);
lvi.SubItems.Add(validElements[i+2]);
lvi.SubItems.Add(validElements[i+3]);
lvi.SubItems.Add(validElements[i+4]);
listView1.Items.Add(lvi);
}