C# 向ListView中添加多列数据的方法

private void button1_Click_1(object sender, EventArgs e)
       {
             方法1(交错数组,简单说是数组的数组)
         
         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 < xxx.Length; i++)
           {
               ListViewItem item = new ListViewItem(xxx[i]);
               listView1.Items.Add(item);
           }
        
           this.listView1.EndUpdate();  

æ¹æ³1
 

方法2(与方法4类似)
           ListViewItem item = new ListViewItem();      //先实例化ListViewItem这个类
           item.Text = "1";                             //添加第1列内容,注意是"Text"
           item.SubItems.Add("2");                      //添加第2列内容
           item.SubItems.Add("3");                      //添加第3例内容
           listView1.Items.Add(item);                   //集体添加进去

æ¹æ³2
 

**方法3**
listView1.Items.Add(new ListViewItem(new string[] { "a", "b","c","d" }));

æ¹æ³3
 

 **方法4  (最好理解)**
 ListViewItem LVI = new ListViewItem("11");      //11是String型,为第1列内容
           LVI.SubItems.Add("22");
           LVI.SubItems.Add("33");
           listView1.Items.Add(LVI);

æ¹æ³4
 

**方法5 (两个数据内容填充到同一行中的多列)**
for (int i = 0; i < 6; i++)
           {
           //这里i需要是字符型的,所以转换一下,i为第1列的内容
               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);
           }

æ¹æ³5
注意:
1.要想此控件显示条目,需要先添加条目,而只能给ListView控件添加基于ListViewItem类的对象,所以使用此类之前,要先实例化这个类,即:
ListViewItem item = new ListViewItem();

2.ListView编程添加标题及行高设置(实例图如上)

public void listview1_caption()
        {
            listView1.Columns.Add(" 位 置 号", 75, HorizontalAlignment.Center);        //第1列标题添加 
            listView1.Columns.Add(" X 值", 75, HorizontalAlignment.Center);            //第2列标题添加 
            listView1.Columns.Add(" Y 值", 75, HorizontalAlignment.Center);            //第3列标题添加 
            listView1.Columns.Add(" 结 果", 75, HorizontalAlignment.Center);           //第4列标题添加 

            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(1, 25);            // 设置行高 25 //分别是宽和高 
            listView1.SmallImageList = imgList;            //这里设置listView的SmallImageList ,用imgList将其撑大
        }


        }

发布了144 篇原创文章 · 获赞 43 · 访问量 132万+

猜你喜欢

转载自blog.csdn.net/qq_25889465/article/details/103366068
今日推荐