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:

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();

// Windows Form section

Data dat = new Data();

public void button1_click(object sender, eventargs e)
   for(int i = 0; i <; i++)
      string s =[i];
      string s2 = 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?
Updated 6-Dec-11 8:41am
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.
RaviRanjanKr 7-Dec-11 2:42am
My 5+
Wonde Tadesse 7-Dec-11 10:56am
Wonde Tadesse 6-Dec-11 15:53pm
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