【问题标题】:How to bind an array of lists with a GridView or a DataList?如何将列表数组与 GridView 或 DataList 绑定?
【发布时间】:2012-10-10 23:49:34
【问题描述】:

我创建了一个列表数组。该数组有七行代表星期几,列表包含医生预约的可用时段。我试图将它与GridViewDataList (更合适的)绑定,但没有成功。

我已经宣布了名单:

List<string>[] list=new List<string>[7]; //An array of 7 lists
                    for (int i = 0; i < 7; i++)
                    {
                        list[i]=new List<string>();
                    }

我在列表中填写了代表与医生预约的可用空档的字符串。

我想要达到的结果是让其中一位医生有空,正如本网站所描述的那样:link

【问题讨论】:

    标签: c# asp.net gridview datasource nested-lists


    【解决方案1】:

    您可以像这样将数组更改为List&lt;List&lt;string&gt;&gt;

    List<List<string>> list = new List<List<string>>();
    

    然后您可以通过以下方式将其绑定到GridView(只需适应您的情况):

    protected void Page_Load(object sender, EventArgs e)
    {
        List<string> cols = GetColDefsFromBackend();
        List<List<string>> rows = GetDataFromBackend();
    
    
        GridView1.DataSource = CreateDataTable(cols, rows);
        GridView1.DataBind();
    }
    
    
    private System.Data.DataTable CreateDataTable(List<string> columnDefinitions, List<List<string>> rows)
    {
        DataTable table = new DataTable();
        foreach (string colDef in columnDefinitions)
        {
            DataColumn column;
            column = new DataColumn();
            column.DataType = typeof(string);
            column.ColumnName = colDef;
            table.Columns.Add(column);
        }
    
    
        // Create DataRow and Add it to table
        foreach (List<string> rowData in rows)
        {
            DataRow row = table.NewRow();
            // rowData is in same order as columnDefinitions
            for (int i = 0; i < rowData.Count; i++)
            {
                row[i] = rowData[i];
            }
            table.Rows.Add(row);
        }
    
    
        return table;
    }
    
    
    /// <summary>
    /// Simulates a StoredProcedureCall which returns
    /// the data in a List with Lists of strings
    /// </summary>
    private List<List<string>> GetDataFromBackend()
    {
        List<List<string>> myData = new List<List<string>>();
        myData.Add(Row(1));
        myData.Add(Row(2));
        myData.Add(Row(3));
        return myData;
    }
    
    
    private List<string> Row(int p)
    {
        List<string> row = new List<string>();
        for (int i = 0; i < 4; i++)
        {
            row.Add(string.Format("Column {0}/{1}", p, i));
        }
        return row;
    }     
    
    private List<string> GetColDefsFromBackend()
    {
        List<string> cols = new List<string>();
        cols.Add("Col1");
        cols.Add("Col2");
        cols.Add("Col3");
        cols.Add("Col4");
        return cols;
    }
    

    来源:Use List&lt;List&lt;string&gt;&gt; as GridView - DataSource

    【讨论】:

    • 您的回复很有帮助。谢谢!
    【解决方案2】:

    非常感谢。这真的很有帮助,我花了一些时间试图理解这种转换背后的逻辑,作为一个新手,我最终得到了一段小钓鱼代码:

     private System.Data.DataTable CreateDataTable(List<string> columnDefinitions, List<List<string>> rows)
        {
            DataTable table = new DataTable();
    
            foreach (string colDef in columnDefinitions)
            {
                DataColumn column;
                column = new DataColumn();
                column.DataType = typeof(string);
                column.ColumnName = colDef;
                table.Columns.Add(column);
            }
    
            for (int i = 0; i < rows[0].Count; i++)
            {
                table.Rows.Add(rows[0][i], rows[1][i], rows[2][i], rows[3][i], rows[4][i], rows[5][i], rows[6][i]);
            }
            return table;
        }
    
        private List<string> GetColDefsFromBackend()
        {
            List<string> cols = new List<string>();
            cols.Add("Monday");
            cols.Add("Tuesday");
            cols.Add("Wednesday");
            cols.Add("Thursday");
            cols.Add("Friday");
            cols.Add("Saturday");
            cols.Add("Sunday");
            return cols;
        }
    

    【讨论】:

    • 我想补充一点,每列的原始长度各不相同,因此我必须在列表中添加一些新的空项目,然后再将其转换为表格。
    • +1 :您根据自己的需要调整代码真是太好了。 :)
    【解决方案3】:

    将列表变成数据表,然后绑定。数组的数组对于清晰的绑定来说有点复杂。

    【讨论】:

      猜你喜欢
      • 2013-08-19
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多