DetailsView binding error empty datatable?

I am using .net 4.5 and I found this strange behavior:

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

result

A collection cannot be null. Parameter Name: c

thrown at dtvTest.DataBind ().

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

Any idea on how to fix / get around it?

Thank you very much

+4
source share
2 answers

I ran into the same problem in a recent project of mine, I solved it by linking empty collectconon lines as Follows, (btw I compiled it in 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 no matter how many columns you add, it still works.

considers

+1
source

Maybe checking for the rows you are asking for?

  if (table.Rows.Count > 0) { DataBind(); } 

In addition, this one may be useful and this solution.

0
source

All Articles