0

我有一个清单:

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,但是如何使用?请给我一个例子如何!

4

2 回答 2

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();
于 2012-10-01T06:49:42.227 回答
0

这是一个非常简单的问题。我不知道我理解的对吗?

        ListView L = new ListView();

        foreach (Person P in resQuery)
        {
            L.Items.Add(P.n1);//if name is a string property
        }
        Controls.Add(L);
于 2012-09-30T15:11:36.817 回答