古詩詞大全網 - 成語查詢 - C#ListView 添加ListViewItem後無法顯示出來?

C#ListView 添加ListViewItem後無法顯示出來?

需要先設置列頭

ColumnHeader ?ch= new ColumnHeader();

ch.Text = "列標題1";?

this.listView1.Columns.Add(ch);

記住幾個關鍵的問題

a、記得添加列既是column

b、記得設置view屬性為deteils

代碼是最好的解釋

private?void?Form1_Load(object?sender,?EventArgs?e)

{

listView1?.View?=?View?.Details?;

ColumnHeader?clh;

clh=new?ColumnHeader?();

clh?.Text?="姓名";

listView1?.Columns?.Add?(clh?);

clh=new?ColumnHeader?();

clh?.Text?="學號";

listView1?.Columns?.Add?(clh?);

clh?=?new?ColumnHeader();

clh?.Text?="性別";

listView1?.Columns?.Add?(clh?);

clh?=?new?ColumnHeader();

clh?.Text?="成績";

listView1?.Columns?.Add?(clh?);

//以上代碼為添加列

//下面添加組。用性別分組

ListViewGroup?grp;

grp=new?ListViewGroup?();

grp.Header?=?"男";

listView1?.Groups?.Add?(grp);

grp=new?ListViewGroup?();

grp.Header?="女";

listView1?.Groups?.Add?(grp);

//下面添加信息

ListViewItem?lvi;

lvi?=new?ListViewItem?();

lvi?.Text?="張三?";

lvi.SubItems.Add?("200813340301");

lvi.SubItems.Add("男");

lvi.SubItems.Add?("90");

lvi.Group?=?listView1.Groups[0];

listView1.Items.Add(lvi);

//?listView1.Groups[0].Items.Add(lvi);

lvi?=new?ListViewItem?();

lvi?.Text?="李華?";

lvi.SubItems.Add?("200813340302");

lvi.SubItems.Add("女");

lvi.SubItems.Add("80");

lvi.Group?=?listView1.Groups[1];

listView1.Items.Add(lvi);

//?listView1.Groups[1].Items.Add(lvi);

}