0

是否可以基于列表创建网格视图?我有以下列表:

ID = 1
Name = John
Zip = 33141
ID = 2
Name = Tim
Zip = 33139

我希望能够使用此列表创建可编辑的网格视图

当我将它绑定到网格视图时,它似乎将所有内容放在一列中,我不知道如何让它将它分成不同的列

这是我设置的DataSource代码GridView

DataTable table = ConvertListToDataTable(personList); 
GridView1.DataSource = table; 
GridView1.DataBind(); 

static DataTable ConvertListToDataTable(List<string> list) 
{ 
    // New table. 
    DataTable table = new DataTable(); 

    // Get max columns. 
    int columns = 7; 

    // Add columns. 
    for (int i = 0; i < columns; i++) 
    { 
        table.Columns.Add(); 
    } 

    // Add rows. 
    foreach (var rd in list) 
    { 
        table.Rows.Add(rd); 
    } 

    return table; 
}
4

1 回答 1

0

这是一个例子:

    private class Person
    {
        int m_iID;
        string m_sName;
        string m_sZip;

        public int ID { get { return m_iID; } }
        public string Name { get { return m_sName; } }
        public string Zip { get { return m_sZip; } }

        public Person(int iID, string sName, string sZip)
        {
            m_iID = iID;
            m_sName = sName;
            m_sZip = sZip;
        }
    }

    private List<Person> m_People;

    private void ConvertListToDataTable(List<Person> People)
    {
        DataTable table = new DataTable();

        DataColumn col1 = new DataColumn("ID");
        DataColumn col2 = new DataColumn("Name");
        DataColumn col3 = new DataColumn("Zip");

        col1.DataType = System.Type.GetType("System.String");
        col2.DataType = System.Type.GetType("System.String");
        col3.DataType = System.Type.GetType("System.String");

        table.Columns.Add(col1);
        table.Columns.Add(col2);
        table.Columns.Add(col3);


        foreach (Person person in People)
        {
            DataRow row = table.NewRow();
            row[col1] = person.ID;
            row[col2] = person.Name;
            row[col3] = person.Zip;

            table.Rows.Add(row);
        }            

        GridView1.DataSource = table;
        GridView1.DataBind();
    }
于 2013-07-31T22:11:13.637 回答