根據自己的應用方式和在網上搜尋的資料,整理一下ListView控件的應用。
ListView控件可以直觀的顯示數據,操作很方便簡單的特點。
一,ListView添加表頭的兩種方法:
1,直接在控件的任務中的編輯列中添加,Name是綁定的code值,Text是表頭的顯示值。
2,在后台代碼中添加表頭,代碼如下:
ColumnHeader ch = new ColumnHeader();//聲明表頭,並創建對象
ch.Text = "111"; //表頭的顯示名稱
ch.Name = "222"; //表頭綁定的code值
ch.Width = 100; //表頭寬度
this.listView_EH.Columns.Add(ch); //添加表頭到ListView中
如果要添加第二列,可以按照上面的方式添加。
二,ListView綁定數據的方法
1,獲取dt或ds后foreach遍歷每一行記錄,然后給ListView的相對於的列附值。這個方法數據源和綁定方法不能分離
注:注意表頭顯示的信息和綁定信息的順序。綁定的順序既是顯示的順序
代碼如下:
//綁定ListView,顯示數據 private void SetListView() { this.listView_EH.Items.Clear(); DataSet ds = qt.GetQuestionTypeInfo(); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { foreach (DataRow dr in ds.Tables[0].Rows) { string strQuestionTypeID = dr["QuestionTypeID"].ToString(); string strQuestionTypeName = dr["QuestionTypeName"].ToString(); int intsort = Convert.ToInt32(dr["sort"].ToString()); string strAnswerTypeName = dr["AnswerTypeName"].ToString(); ListViewItem lvItem = new ListViewItem(); lvItem.Text = intsort.ToString(); lvItem.Tag = dr; lvItem.SubItems.Add(strQuestionTypeName.ToString()); lvItem.SubItems.Add(strAnswerTypeName.ToString()); this.listView_EH.Items.Add(lvItem); } } }
然后ListView控件讀取當前選擇行的代碼時可以這樣
//讀取ListView的某行的信息 private void listView_EH_Click(object sender, EventArgs e) { if (this.listView_EH.Items.Count <= 0) return; if (this.listView_EH.SelectedItems.Count <= 0) return; int index = this.listView_EH.SelectedItems[0].Index; if (index < 0) return; DataRow dr = (DataRow)(this.listView_EH.Items[index].Tag); this.txt_questiontypeID.Text = dr["QuestionTypeID"].ToString(); this.num_sort.Text = dr["sort"].ToString(); this.txt_questiontypename.Text = this.listView_EH.SelectedItems[0].SubItems[1].Text.ToString(); this.com_answertype.Text = dr["AnswerTypeName"].ToString(); }
2,獲取dt或者ds后,然后for數據源的每一行的每一列給ListView列附值。這個方法數據源和綁定方法可以分離,但是綁定的時候要注意select的數據源的順序和顯示的順序是一樣的,還有就是顯示列和表頭對應。注意第二次循環時表頭的對應。
代碼如下:
//綁定ListView,顯示數據 private void SetListView() { this.listView_EH.Items.Clear(); DataSet ds = qt.GetQuestionTypeInfo(); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { ListViewItem lv = null; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { lv = new ListViewItem(ds.Tables[0].Rows[i][1].ToString()); lv.Tag = ds.Tables[0].Rows[i][0].ToString(); for (int j = 2; j < ds.Tables[0].Columns.Count; j++) { lv.SubItems.Add(ds.Tables[0].Rows[i][j].ToString()); } this.listView_EH.Items.Add(lv); } } }
然后ListView控件讀取當前選擇行的代碼時這樣
//讀取ListView的某行的信息 private void listView_EH_Click(object sender, EventArgs e) { this.num_sort.Text = this.listView_EH.SelectedItems[0].SubItems[0].Text; this.txt_questiontypename.Text = this.listView_EH.SelectedItems[0].SubItems[1].Text; this.com_answertype.Text = this.listView_EH.SelectedItems[0].SubItems[2].Text; }
三,解決ListView顯示的時候隱藏一列,當讀取某行的信息時,可以顯示隱藏列的值。
該問題描述的是當ListView在第二中方法綁定時,顯示的時候將某列值隱藏,當讀取改行的信息時可以讀取隱藏值。
在第二種方法中。lv = new ListViewItem(ds.Tables[0].Rows[i][1].ToString());是ListView中顯示的第一列數據。
而lv.Tag=ds.Tables[0].Rows[i][0].ToString();就是隱藏列。
當讀取改行數據的時候即可通過
this.txt_questiontypeID.Text = this.listView_EH.SelectedItems[0].Tag.ToString();
獲得該隱藏列的值。
//Tag可以付任何對象的值,比如字符串,數值型...我經常用該項保存調用數據庫時不用顯示的編號信息。