如何使用GridView或DataList绑定列表数组?

时间:2012-10-20 15:31:04

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

我创建了一系列列表。该阵列有七行代表星期几,列表包含医生预约的可用插槽。我试图用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

3 个答案:

答案 0 :(得分:1)

您可以将数组更改为List<List<string>>,如下所示:

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<List<string>> as GridView - DataSource

答案 1 :(得分:1)

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

 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;
    }

答案 2 :(得分:0)

将List转换为数据表然后绑定。数组数组很难清楚地绑定。