Introduction
ListView control can directly view data
information that makes operations much easier. This article will show you some
usages of ListView on binding and reading in WinForm for C#. Wish this would be
useful for some of you.
Use ListView to Add Table Header
- Directly add table header in control edit column. Name is binding code value and Text is the value displays in the header.
- Add table header in codes.
ColumnHeader ch = new ColumnHeader(); ch.Text = "111"; ch.Name = "222"; ch.Width = 100; this.listView_EH.Columns.Add(ch);
If we need add the 2nd column, we can also use this method.
ListView control binding data information
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);
}
}
}
Then, the codes of ListView reading selected line can be this:
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();
}
After acquire dt or ds, for each row and each column of data source and give value to ListView column. By using this method, data source and binding method can be separated
Note: The order of selected data source and the order displayed is the same. And displayed row and table header is related.
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);
}
}
}
Then, the codes that ListView reading current selected row:
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;
Hide Column
This solution describes that in the second method above, some columns which were hidden can be read.
In the second method: lv = new ListViewItem(ds.Tables[0].Rows[i][1].ToString()); means the first row data information in ListView. lv.Tag=ds.Tables[0].Rows[i][0].ToString(); stands for the hidden column
When read the hidden column, we can use the code below to acquire the value
this.txt_questiontypeID.Text = this.listView_EH.SelectedItems[0].Tag.ToString();
Recommend
Export Data from ListView without Automation
Export Data from ListView for ASP.NET without Automation