我有一个清单:
public class Person
{
public decimal n1 { get; set; }
public decimal n2 { get; set; }
}
List<Person> dbItems = new List<Person>();
public void getinfo(int id)
{
using (var con = new SqlConnection(connectionString))
{
con.Open();
string query = "select * from Zboruri where cod_decol_ateriz = " + id;
var resQuery = new List<Person>();
using (var com = new SqlCommand(query, con))
{
var reader = com.ExecuteReader();
int a = reader.GetOrdinal("number1");
int b = reader.GetOrdinal("number2");
while (reader.Read())
{
resQuery.Add(new Person {
n1 = reader.GetDecimal(a),
n2 = reader.GetDecimal(b)
});
}
}
}
我希望在列表视图中显示此列表,但我不知道如何。我知道我必须使用foreach
,但是怎么样?请举个例子!
答案 0 :(得分:0)
ListView L = new ListView();
foreach (Person P in resQuery)
{
L.Items.Add(P.n1);//if name is a string property
}
Controls.Add(L);
答案 1 :(得分:0)
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<asp:Label Text='<%#Eval("n1")%>' runat="server"></asp:Label>
</ItemTemplate>
</asp:ListView>
public class Person
{
public string n1 { get; set; }
public string n2 { get; set; }
}
List<Person> list = new List<Person>();
list.Add(new Person { n1 = "A", n2 = "A" });
list.Add(new Person { n1 = "B", n2 = "B" });
list.Add(new Person { n1 = "C", n2 = "C" });
list.Add(new Person { n1 = "D", n2 = "D" });
list.Add(new Person { n1 = "E", n2 = "E" });
ListView1.DataSource = list;
ListView1.DataBind();