C# 将 ASP.NET GridView 控件绑定到字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/945701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding an ASP.NET GridView Control to a string array
提问by Michael Kniskern
I am trying to bind an ASP.NET GridView
control to an string
array and I get the following item:
我试图将一个 ASP.NETGridView
控件绑定到一个string
数组,我得到以下项目:
A field or property with the name 'Item' was not found on the selected data source.
在所选数据源中找不到名为“项目”的字段或属性。
What is correct value I should use for DataField property of the asp:BoundField column in my GridView control. Here is my source code:
我应该为 GridView 控件中 asp:BoundField 列的 DataField 属性使用什么正确值。这是我的源代码:
ASPX page
ASPX 页面
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Item" />
<asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="Click Me!" />
</Columns>
</asp:GridView>
Code Behind:
背后的代码:
string[] MyArray = new string[1];
MyArray[0] = "My Value";
MyGridView.DataSource = MyArray;
MyGridView.DataBind();
UPDATE
更新
I need to have the AutoGenerateColumns
attribute set to false
because I need to generate additional asp:CommandField
columns. I have updated my code sample to reflect this scenario
我需要将AutoGenerateColumns
属性设置为,false
因为我需要生成额外的asp:CommandField
列。我已经更新了我的代码示例以反映这种情况
采纳答案by Michael La Voie
One method is to pass it a class with a single, named field. That way, you can give it a name.
一种方法是将一个具有单个命名字段的类传递给它。这样,您就可以为其命名。
public class GridRecord
{
public string MyValue { get; set; }
}
Then convert your string array to a list of the class
然后将您的字符串数组转换为类的列表
string[] MyArray = new string[1];
MyArray[0] = "My Value";
List<GridRecord> MyList = (
from ar in myArray
select new GridRecord
{
MyValue = ar
}).ToList();
MyGridView.DataSource = MyList;
MyGridView.DataBind();
Now you can name your DataField property
现在您可以命名您的 DataField 属性
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="MyValue" />
</Columns>
</asp:GridView>
回答by Matthew Jones
Try replacing the BoundField with a TemplateField like so:
尝试用 TemplateField 替换 BoundField ,如下所示:
<asp:TemplateField HeaderText="String Value">
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
</asp:TemplateField>
BTW I lifted this from another question
顺便说一句,我是从另一个问题中提出来的
回答by Robert Harvey
Michael,
迈克尔,
The line of code
代码行
<asp:BoundField DataField="Item" />
expects a column with the name of "Item," which you would have if you were binding to one of the DataSource controls such as SqlDataSource, ObjectDataSource, or LinqDataSource. Since you are binding to an IEnumerable, you have no such name.
需要名称为“Item”的列,如果您绑定到数据源控件之一(如 SqlDataSource、ObjectDataSource 或 LinqDataSource),您将拥有该列。由于您绑定到 IEnumerable,因此您没有这样的名称。
回答by Luk
After hours of search, I finally found that there is a special DataField for this case: "!"
经过几个小时的搜索,我终于发现这个案例有一个特殊的DataField:“ !”
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="!" />
</Columns>
</asp:GridView>
I hope it'll help someone one day :)
我希望有一天它会帮助某人:)
回答by Joshua Coppersmith
Here is a complete example using the old DataGrid...so it appears that the "!" trick has widespread implementation. This worked under ASP.NET in VS2008. Of course, just substitute the right element names to use a GridView.
这是一个使用旧 DataGrid 的完整示例......所以看起来“!” 技巧有广泛的实施。这在 VS2008 中的 ASP.NET 下工作。当然,只需替换正确的元素名称即可使用 GridView。
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="WebApplication2._Default"
%>
<%@Import
Namespace="System.Collections.Generic"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/C#" runat="server">
void initList()
{
List<String> myList = new List<String>();
myList.Add("Hello");
myList.Add("Chatting");
myList.Add("Goodbye");
Grid1.DataSource = myList;
Grid1.DataBind();
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<%initList(); %>
<asp:DataGrid runat="server" ID="Grid1" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="!" DataFormatString="Data: {0}" HeaderText="Dyad"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
So as a GridView the inner section would be
因此,作为 GridView,内部部分将是
<asp:GridView runat="server" ID="Grid1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="!" DataFormatString="Data: {0}" HeaderText="Dyad"/>
</Columns>
</asp:GridView>
If you switch back and forth, notice that VS2008 (at least) cannot re-declare the control type in the Designer.cs class, so you'll have to change that by hand if just editing the element names.
如果来回切换,请注意 VS2008(至少)无法在 Designer.cs 类中重新声明控件类型,因此如果只是编辑元素名称,则必须手动更改它。