Click here to Skip to main content
Licence CPOL
First Posted 3 Jan 2012
Views 4,298
Bookmarked 5 times

Binding and Reading Usage of ListView in WinForm for C#

By | 3 Jan 2012 | Article
Introduce some usages of ListView on binding and reading in WinForm for C#

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();//declare header and create object 
                        ch.Text = "111"; //header name
                        ch.Name = "222"; //binding code value
                        ch.Width = 100; //header width 
                        this.listView_EH.Columns.Add(ch); //add header into ListView
    

    If we need add the 2nd column, we can also use this method.

ListView control binding data information

  • After acquire dt or ds, foreach records of each line. Then, give value to the listView relative column. By using this method, data source and binding methods can’t be separated.
  • Note:The binding information order is the same as table header displayed order

    //Bind ListView, display 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:

     //Read ListView line information       
       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.

    //Binding ListView, display 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)     
             {           
           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:

      //Read ListView line information    
         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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

loveyou999


e-iceblue
United States United States

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAuto create columns PinmemberMehdi Gholam20:10 3 Jan '12  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 4 Jan 2012
Article Copyright 2012 by loveyou999
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid