SharePoint list.items.GetDataTable 列名称与字段名称不匹配

发布于 2024-07-08 03:23:21 字数 472 浏览 9 评论 0原文

我将 SPGridView 绑定到 SPList。 正如代码示例所示,我使用以下代码基于列表创建数据视图。

dim data as DataView = myList.Items.GetDataTable.DefaultView
grid.DataSource = data
etc...

我发现结果数据视图中的列名称并不总是与 SPList 中定义的源字段匹配。 例如,我有名为

  • Description
  • ReportItem
  • ReportStatus

    的列

    这些显示在结果数据视图中,列名称如

  • ReportType0
  • ReportStatus1

这让我认为我定义了重复的字段名称,但这似乎并不存在是这样的。

似乎我在这里错过了一些基本的东西? 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪是无色的血 2024-07-15 03:23:22

GetDataTable 方法返回 internalName (或 staticName - 我无法确定是哪一个,但它们通常是相同的)表示形式列,而不是您在 Web 界面中看到的 Title 表示形式。 我相信 GetDataTable 在幕后执行 CAML 查询,并且您必须使用该 internalName 在 CAML 中进行字段引用。

博客更详细地讨论它。

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.

冰葑 2024-07-15 03:23:22

所以我在我的博客上发布了这一点,但我写了一个你可以使用的小实用方法,在你获取数据表之后,它基本上将 DataTable 中的列名称重新映射为其友好名称。

DataTable table = list.GetItems(list.DefaultView).GetDataTable();
foreach(DataColumn column in table.Columns)
{
   column.ColumnName = list.Fields.GetFieldByInternalName(column.ColumnName).Title;
}

希望有帮助!

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!

你又不是我 2024-07-15 03:23:22

您还可以做的(如果您使用的是 .NET 3.5)是使用匿名类型并对其进行绑定。 如果您这样做,您可能还想使用 Linq 数据源。
我在此处发表了一篇文章来解释这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文