今天搞Winform,有串數據需要綁定到TabControl里面,原來用datatable,組長說這玩意會有問題不讓用,菜鳥實在不會,百度查的Listview用法,寫了個數組進去綁定
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[,] numbers = new string [,] { { "1"," 2" ," 3"}, {" 4", "5", "6 "} };
for (int i = 0; i < numbers.GetLength(0); i++)
{
ListViewItem additem = new ListViewItem();
additem.SubItems.Clear();
additem.SubItems.Add(numbers[i,0]);
additem.SubItems.Add(numbers[i,1]);
additem.SubItems.Add(numbers[i,2]);
listView1.Items.Add(additem);
additem.EnsureVisible();
}
}
}
}
數據是綁進去了,就是錯位啊,我滴爹爹

原因好像是這玩意默認從1開始的。怎么解決,還未知。
方法二:
ListViewItem cj;
cj = new ListViewItem(numbers[i, 0]);
cj.SubItems.Add(numbers[i, 1]);
cj.SubItems.Add(numbers[i,2]);
listView1.Items.Add(cj);
