C# WinForm中ListView 的繫結和讀取方法
根據自己的應用方式和在網上搜尋的資料,整理一下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可以付任何物件的值,比如字串,數值型...我經常用該項儲存呼叫資料庫時不用顯示的編號資訊。