4

I am using .net 4.5 and I found this odd behaviour:

Markup:

<asp:DetailsView ID="dtvTest" AutoGenerateRows="true" DefaultMode="Insert" runat="server" /> 

Code:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable("Test");
    dt.Columns.Add("Column", typeof(string));
    // If I uncomment the line it works!
    // dt.Rows.Add("row 1");
    dtvTest.DataSource = dt;
    dtvTest.DataBind(); 
}

the result is

Collection cannot be null. Parameter name: c

thrown on dtvTest.DataBind().

If there is at least one row it works!! (see the commented line).

Any idea on how to fix/work around it?

Many thanks

4

1 回答 1

1

我在我最近的一个项目中遇到了同样的问题,我通过将空行集合绑定为 Follows 来解决它,(顺便说一句,我在你的解决方案中编译了它,它工作得很好)

 protected void Page_Load(object sender, EventArgs e)
    {
       DataTable dt = new DataTable("Test");
        dt.Columns.Add("Column", typeof(string));

        // If I uncomment the line it works!
        // dt.Rows.Add("row 1");

       dt.LoadDataRow(new string[1],true);
        dtvTest.DataSource = dt;

        dtvTest.DataBind();

    }

而且无论您添加多少列,它仍然有效。

问候

于 2013-05-03T17:28:19.220 回答