ListViewにデータの複数の列を追加するC#メソッド



C Method Adding Multiple Columns Data Listview



private void button1_Click_1(object sender, EventArgs e) { Method 1 (jagged array, simply an array of arrays) string[][] xxx = new string[10][] xxx[0] = new string[] { '1', '2', '3' ,'4'} xxx[1] = new string[] { '4', '5', '6' } xxx[2] = new string[] { '7', '8', '9' } xxx[3] = new string[] { '10', '11', '12' } xxx[4] = new string[] { '13', '14', '15' } xxx[5] = new string[] { '16', '17', '18' } xxx[6] = new string[] { '19', '20', '21' } xxx[7] = new string[] { '22', '23', '24' } xxx[8] = new string[] { '25', '26', '27' } xxx[9] = new string[] { '28', '29', '30' } for (int i = 0 i

æ&sup1æ³1

Method 2 (similar to Method 4) ListViewItem item = new ListViewItem() //First instantiate the ListViewItem class Item.Text = '1' //Add the content in column 1, pay attention to 'Text' Item.SubItems.Add('2') //Add the second column content Item.SubItems.Add('3') item.SubItems.Add('3') //Add the content of the third example ListView1.Items.Add(item) //Add collectively

æ&sup1æ³2



**Method 3** listView1.Items.Add(new ListViewItem(new string[] { 'a', 'b','c','d' }))

æ&sup1æ³3

**Method 4 (best to understand)** ListViewItem LVI = new ListViewItem('11') //11 is String type, which is the first column content LVI.SubItems.Add('22') LVI.SubItems.Add('33') listView1.Items.Add(LVI)

æ&sup1æ³4



**Method 5 (Two data contents are filled into multiple columns in the same row)** for (int i = 0 i <6 i++) { //Here i needs to be a character type, so convert it, i is the content of the first column ListViewItem item = new ListViewItem(Convert.ToString(i)) string[] first = { 'a', 'b', 'c' } string[] second = { '1', '2', '3' } item.SubItems.AddRange(first) item.SubItems.AddRange(second) listView3.Items.Add(item) }

æ&sup1æ³5
注意:
1.このコントロールでアイテムを表示する場合は、最初にアイテムを追加する必要がありますが、リストビューコントロールに追加できるのはListViewItemクラスに基づくオブジェクトのみであるため、このクラスを使用する前に、まずこのクラスをインスタンス化する必要があります。これは:
ListViewItem item = new ListViewItem()

2.タイトルと行の高さの設定を追加するListViewプログラミング(上の図の例)

public void listview1_caption() { listView1.Columns.Add('Position number', 75, HorizontalAlignment.Center) //Add the first column title listView1.Columns.Add('X value', 75, HorizontalAlignment.Center) //Add the second column title listView1.Columns.Add(' Y value', 75, HorizontalAlignment.Center) //Add the third column title listView1.Columns.Add('Result', 75, HorizontalAlignment.Center) //Add the title of column 4 ImageList imgList = new ImageList() imgList.ImageSize = new Size(1, 25) // set line height 25 // width and height respectively listView1.SmallImageList = imgList //Set the SmallImageList of listView here, and use imgList to enlarge it }


}