簡體   English   中英

C# 如何添加<list>字符串到列表視圖</list>

[英]C# How to add <list>string into Listview

我有List<string> ,其中包含 cmd 中 .netstat 命令的已解析 output 你看到的列表是這樣的:

    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);
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM