结合一个空的DataTable时,DetailsView控件的错误? [英] DetailsView bug when binding an empty datatable?

查看:198
本文介绍了结合一个空的DataTable时,DetailsView控件的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET 4.5,我发现这个奇怪的行为:

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

标记:

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

结果是

集合不能为空。参数名:C

Collection cannot be null. Parameter name: c

扔到dtvTest.DataBind()。

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?

非常感谢

推荐答案

我曾经面临相同的概率在我最近我通过结合空行解决了它的项目colleciton如下,(顺便说一句我在乌拉圭回合的解决方案,编译它,它工作得很好)

I have faced the same prob in a recent project of mine I solved it by binding empty rows colleciton as Follows , (btw I compiled it at ur solution and it works just fine)

 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();

    }

,也不管有多少列添加它仍然有效。

and also no matter how many columns you add it still works.

关于

这篇关于结合一个空的DataTable时,DetailsView控件的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆