DetailsView bug when binding an empty datatable?

拥有回忆 提交于 2020-01-14 12:27:31

问题


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


回答1:


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.

regards



来源:https://stackoverflow.com/questions/16303018/detailsview-bug-when-binding-an-empty-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!