将Listview数据绑定到列表 [英] Databinding Listview to List
问题描述
我有一个包含多列的listview控件.每列均由List<t>
对象填充,该对象从数据库查询中接收项目.根据特定列值的内容为行着色的行.我想知道如何将List<t>
绑定到列表视图?这是最有效的方法吗?我应该改用dataTable吗?如果是这样,我该如何绑定它并根据列/行的内容为列着色.
这是我的代码示例:
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();
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;
}
}
}
将listview绑定到数据表或绑定列表是否更有效,这样我就不必遍历每一行?
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?
推荐答案
值得一看的是 ^ ] CodeProject答案.
It is worth to see this[^] CodeProject answer.
尝试
绑定ListView的数据 [
Try
Data binding a ListView[^]
这篇关于将Listview数据绑定到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!