SharePoint list.items.GetDataTable column names not match field names
-
08-07-2019 - |
Question
I am binding an SPGridView to a SPList. As code samples suggest, I am using the following code to create a dataview based on the list.
dim data as DataView = myList.Items.GetDataTable.DefaultView
grid.DataSource = data
etc...
What I am finding is that the column names in the resulting dataview do not always match the source fields defined in the SPList. For example I have columns named
- Description
- ReportItem
ReportStatus
these show up in the resulting dataview with column names like
- ReportType0
- ReportStatus1
This leads me to think that I have duplicate field names defined, but that does not seem to be the case.
Seems like I am missing something fundamental here? Thanks.
Solution
The GetDataTable
method is returning the internalName
(or staticName
-- I can't remember for sure which but they are frequently the same) representation of the columns, rather than the Title
representation, which is what you see in the Web interface. I believe GetDataTable
does a CAML query under the covers, and you have to use that internalName
for field references in CAML.
This blog talks about it in a little more detail.
OTHER TIPS
So I posted about this on my blog, but I wrote a little utility method that you can use, right after you get the data table it basically remaps the column names in the DataTable to their friendly names.
DataTable table = list.GetItems(list.DefaultView).GetDataTable();
foreach(DataColumn column in table.Columns)
{
column.ColumnName = list.Fields.GetFieldByInternalName(column.ColumnName).Title;
}
Hope that helps!
What you also could do (if you´re using .NET 3.5) is to use an anonymous type and bind against that. If you´re doing this you might wanna go with a Linq DataSource as well. I have made a post that explains this here.