Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi,
i want to know how to add and fill columns to listview.
Posted
Comments
Sandeep Mewara 26-Nov-10 12:21pm    
Tried Google?

Well, have a look at all about listview with sample here[^].
 
Share this answer
 
Using a DataTable:

public static void LoadListView(ListView lv, DataTable table)
{
   if(table != null)
   {
      for (int i = 0; i < table.Columns.Count; i++)
      {
         // Iterate through each column in datatable and add column to listview
         ColumnHeader header = new ColumnHeader();
         header.Text = table.Columns[i].ColumnName;
         lv.Columns.Add(header);
      }

      // Iterate through each row in datatable now
      foreach (DataRow row in table.Rows)
      {
         // Create list view item using row's main item
         ListViewItem lvi = new ListViewItem(row[0].ToString());

         // Add the row's subitems to list view item
         for(int i = 1; i < table.Columns.Count; i++)
         {
            item.SubItems.Add(row[i].ToString());
         }

         // Add list view item into the list view
         lv.Items.Add(lvi);
      }
   }
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900