views:

2201

answers:

3

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.

+2  A: 

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.

Abs
+2  A: 

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!

A: 

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.

Johan Leino