Click here to Skip to main content
14,920,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listview control with multiple columns. Each column is populated by a List<t> object that receives it items from a DB query. rows colored according to the contents of specific column values. I wanted to know how can I bind the List<t> to the listview? Is this the most efficient to go about this? Should I be using a dataTable instead? If so how do I bind it and color the columns according to column/row contents.

Here's an example of my code:

C#
class Data
{
  public List<string> info = new List<string>();
  public List<string> info2 = new List<string>();

  public void GetData()
  {
     // create ADO.NET objects i.e. connection, command, dataReader, etc...
     // connect to database 
     // execute query
    
    SqlDataReader rdr = sqlCmd.ExecuteReader();
    
    while(rdr.Read())
    {
      info.Add(rdr["data1"]);
      info2.Add(rdr["data2"]);
    }
  }

// Windows Form section

Data dat = new Data();

public void button1_click(object sender, eventargs e)
{
   dat.GetData();
  
   for(int i = 0; i < dat.info.Count; i++)
   {
      string s = dat.info[i];
      string s2 = dat.info2[i];
     
      listview.Items[i].Add(dat.info[i]));
      listview.Items[i].SubItems.Add(dat.info2[i]);

     if(s == "xyz")
     {
       listview.Items[i].BackColor = Color.Green;
     }

     if(s2 == "rst")
     {
       listview.Items[i].Backcolor = Color.Red;
     }
   }
}

Wouldn't it be more efficient to bind the listview to a datatable or bindinglist so that way i wouldn't have to iterate though each row? how do I create a customListview for this and how would I go about the coloring scheme?
Posted
Updated 6-Dec-11 8:41am
v2
Comments
Wendelius 6-Dec-11 14:42pm
   
Pre tags added
Wonde Tadesse 6-Dec-11 15:08pm
   
Tag it, WinForm, Asp.Net ...

It is worth to see this[^] CodeProject answer.
   
Comments
RaviRanjanKr 7-Dec-11 2:42am
   
My 5+
Wonde Tadesse 7-Dec-11 10:56am
   
Thanks
   
Comments
Wonde Tadesse 6-Dec-11 15:53pm
   
5+
RaviRanjanKr 7-Dec-11 2:41am
   
Thanks :)

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