Why is the System.Data.DataTable.ImportRow method inserting a row with no columns?

StackOverflow https://stackoverflow.com/questions/8729083

  •  14-04-2021
  •  | 
  •  

Вопрос

I have some C# code that looks something like this

System.Data.DataRow row;
var table = new System.Data.DataTable();

// code that populates the DataRow...
table.ImportRow(row);

After I call the ImportRow function, a row is inserted into the table, but it has no columns. I've examined the DataRow after it gets populated and it contains all the columns and data that it is supposed to, so that's not the problem.

The documentation on the DataTable.ImportRow method states that, "If the DataRow that is passed as a parameter is in a detached state, it is ignored, and no exception is thrown." I checked the DataRowState of the row I'm trying to insert and it's not detached (it's unchanged) so that's not the problem either.

Does anyone have any ideas as to what's going wrong here?

Это было полезно?

Решение

How are you creating the DataRow?

The important thing here is that the schema needs to be defined on the table, and be compatible with the row.

You can create your row like so:

var row = table.NewRow(); 
//populate....
table.Rows.Add(row);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top