【问题标题】:asp:GridView DataBinding: 'System.Data.DataTable' does not allow indexed accessasp:GridView DataBinding: 'System.Data.DataTable' 不允许索引访问
【发布时间】:2017-06-20 00:33:09
【问题描述】:

我不断收到以下“System.ArgumentException”错误,内容为

DataBinding:“System.Data.DataTable”不允许索引访问。

在我的 GridView 中。当我运行项目时,它会进入 html 标签<asp:Label ID="lblTargetName" runat="server" Text='<%# Eval("[TargetName]") %>'></asp:Label>

这是 HTML:

<asp:GridView>
    <asp:TemplateField HeaderText="TargetName" SortExpression="TargetName">
        <ItemTemplate>
            <asp:Label ID="lblTargetName" runat="server" Text='<%# Eval("[TargetName]") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</asp:GridView>

以及后面绑定网格的代码:

protected void UpdateGridview()
{
string PlanningType = DropDownList4.SelectedValue.ToString();
string ProductionYear = null;
//SqlDataSource sds = new SqlDataSource();
SqlConnection con = new SqlConnection(DatabaseConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

//sds = Page.FindControl("SqlDataSource1") as SqlDataSource;

if (DropDownList5.SelectedValue != "")
    ProductionYear = DropDownList5.SelectedValue.ToString();

try
{
    if (ProductionYear != null)
    {
        using (con)
        {
            con.Open();

            SqlCommand cmd = new SqlCommand("sp_GetSUPPExcelImport", con);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PlanningType", SqlDbType.VarChar));
            cmd.Parameters["@PlanningType"].Value = PlanningType.ToString();
            cmd.Parameters.Add(new SqlParameter("@ProductionYear", SqlDbType.VarChar));
            cmd.Parameters["@ProductionYear"].Value = ProductionYear;                        

            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);

            GridView1.DataSource = ds.Tables;
            GridView1.AllowPaging = true;
            GridView1.DataBind();
        }
    }
}
catch (Exception ex)
{
    Label1.ForeColor = Color.Red;
    Label1.Text = ex.Message.ToString();
}
finally
{
    if (da != null)
        da.Dispose();

    if (ds != null)
        ds.Dispose();

    if (con != null)
    {
        con.Close();
        con.Dispose();
    }
}

【问题讨论】:

  • 您正在将一组数据表分配给 gridview 数据源。那是行不通的。您需要从数据集中分配其中一个表,例如GridView1.DataSource = ds.Tables[0];。之后,您还应该从 eval 中删除 []:Text=''。如果它仍然不起作用,您应该告诉我们更多关于存储过程返回的内容。
  • 感谢您的帮助。

标签: c# asp.net gridview data-binding visual-studio-2015


【解决方案1】:

您的 GridView 也缺少其 ID 和 runat 命令。

&lt;asp:GridView runat="server" ID="GridView1"&gt;

【讨论】:

  • 对不起,我忘记在我的示例中添加 ID 和 runat,但我确实有这 2 项。
【解决方案2】:

我知道这是一个迟到的回应,但我今天遇到了这个异常,这是我发现处理它的极少数地方之一。这可能有助于其他人找出他们的解决方案。

此问题与从 SQL Server 复制数据元素以及“[]”有关。在问题中,OP 尝试绑定到标签并且数据绑定错误被抛出“不允许索引访问”。

注意 Eval() 语句中的括号。去掉括号,一切都会好起来的。

【讨论】:

  • 是的,我也这样做了,但我没有注意到
猜你喜欢
  • 2022-07-11
  • 1970-01-01
  • 2012-03-23
  • 2020-01-21
  • 2011-07-28
  • 2020-05-03
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多