在容器中找不到数据项。容器必须实现IDataItemContainer,或者具有名为DataItem的属性

时间:2014-10-29 06:19:32

标签: c# asp.net arrays gridview

我正在尝试将字符串数组绑定到网格视图。使用下面给出的代码显示错误"A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem."请帮我找到合适的解决方案。谢谢。

代码:

protected void ddlCircle_SelectedIndexChanged(object sender, EventArgs e)
{
    ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter cd;
    cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter();
    DataTable dt = new DataTable();
    dt = cd.GetAvailableData(ddlCircle.SelectedValue); // Getting details of unassigned site

    int x, y; //z;

    DataTable dt3 = new DataTable();
    dt3 = cd.GetTeam();
    y = dt3.Rows.Count;

    x = dt.Rows.Count; // counting the unassinged sites

    DataTable dt2 = new DataTable();
    dt2 = cd.GetAssignTeam(x);           //Getting team based on count

    string[] arr = new string[dt2.Rows.Count];
    int i = 0;
    foreach (DataRow r in dt2.Rows)
    {
        arr[i] = r["Team"].ToString(); // assigning available team to array
        i++;
    }

    string[] strArr = new string[100]; // another array to copy arr values.

    i = 0; int j = 0;
    while (j <= x)
    {
        strArr[j]=  arr[i] ; // copying the arr[] values into strArr[] based on count.
        i++;
        j++;

        if (i == 3)
        {
            i = 0;
        }
    }

    GridView2.DataSource = strArr;
    GridView2.DataBind(); // error popup here
}

3 个答案:

答案 0 :(得分:3)

定义一个GridView列,使其绑定到Team的{​​{1}}列,并将DataTable直接指定给DataTable GridView。然后DataSource转到DataBind

答案 1 :(得分:1)

将数组绑定到DataGrid就像把香蕉放在蛋盘中一样。请你必须根据datagrid绑定一个具有结构的源。正如@Konstantin D - Infragistics

所建议的那样

答案 2 :(得分:0)

现在gridview显示strArr [j]数组值

protected void ddlCircle_SelectedIndexChanged(object sender, EventArgs e)
{
    ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter cd;
    cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter();
    DataTable dt = new DataTable();
    dt = cd.GetAvailableData(ddlCircle.SelectedValue); // Getting details of unassigned site

    int x, y; //z;

    DataTable dt3 = new DataTable();
    dt3 = cd.GetTeam();
    y = dt3.Rows.Count;

    x = dt.Rows.Count; // counting the unassinged sites

    DataTable dt2 = new DataTable();
    dt2 = cd.GetAssignTeam(x);           //Getting team based on count

    string[] arr = new string[dt2.Rows.Count];
    int i = 0;
    foreach (DataRow r in dt2.Rows)
    {
        arr[i] = r["Team"].ToString(); // assigning available team to array
        i++;
    }

    string[] strArr = new string[x+1]; // another array to copy arr values.

    i = 0; int j = 0;
    while (j <= x)
    {
        strArr[j]=  arr[i] ; // copying the arr[] values into strArr[] based on count.
        i++;
        j++;

        if (i == 3)
        {
            i = 0;
        }
    }

    GridView2.DataSource = strArr;
    GridView2.DataBind();
}