C# 将通用列表数组绑定到 GridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/619477/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:30:24  来源:igfitidea点击:

Binding Generic List Array to GridView

c#asp.net.netlistgridview

提问by

Hi I have a List which returns an array of "Question". My question is how can I bind this to a grid view? When I try to call Question.Ordinal I get that it does not exist in the data source. I am using the following code:

嗨,我有一个列表,它返回一个“问题”数组。我的问题是如何将其绑定到网格视图?当我尝试调用 Question.Ordinal 时,我发现它不存在于数据源中。我正在使用以下代码:

GridView1.DataSource = myList.GetQ();
GrdiView1.DataBind();

myList.GetQ() returns a List which is an array of "Question".

myList.GetQ() 返回一个 List,它是一个“问题”数组。

When I set the column DataField to "!" I get the object Question. My question is how can I get the objects property? I tried "!.Ordinal" does not work. I was reading this post for reference, here, any help is greatly appreciated, thanks.

当我将列 DataField 设置为“!”时 我得到了对象问题。我的问题是如何获得对象属性?我试过“!.Ordinal”不起作用。我正在阅读这篇文章以供参考,在这里,非常感谢任何帮助,谢谢。

回答by rjzii

Try using the following syntax:

尝试使用以下语法:

<%# ((MyObject)Container.DataItem).MyField %>

回答by eglasius

Just set it directly to Ordinal, as the first examples in the post you just linked to:

只需将其直接设置为 Ordinal,作为您刚刚链接到的帖子中的第一个示例:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
   <asp:BoundField HeaderText="ID" DataField="CustId" />
   <asp:BoundField HeaderText="Name" DataField="Name" />
   <asp:BoundField HeaderText="City" DataField="City" />
</Columns>
</asp:GridView>

Say:

说:

<asp:BoundField HeaderText="A Header" DataField="APropertyOfQuestion" />

回答by David

you must be define the property member of class, as a propery ie

您必须将类的属性成员定义为属性,即

public string ProductName
{
    get
    {
        return _productName;
    }
    set { }

}

Or VB

或VB

public property ProductName() as string set ..

公共属性 ProductName() 作为字符串集 ..

get...

得到...

end property

最终财产

important: Is Required defined get method

重要:是否需要定义 get 方法