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" };
listview控件在哪里
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();
⽅法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); //集体添加进去
**⽅法3**
listView1.Items.Add(new ListViewItem(new string[] { "a", "b","c","d" }));
**⽅法4 (最好理解)**
ListViewItem LVI = new ListViewItem("11"); //11是String型,为第1列内容 LVI.SubItems.Add("22");
LVI.SubItems.Add("33");
listView1.Items.Add(LVI);
**⽅法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);
}
注意:
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将其撑⼤ }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论