什么是绑定到字符串数组的gridview的dataKeyName? [英] What is the dataKeyName for a gridview bound to a string array?
问题描述
你不能,唯一的属性System.String有Length,并且DataKeyName需要你有的对象属性绑定到。要回答第二个问题,下面是从GridViewRow获取字符串值的示例。
在您的ASPX文件中:
< asp:GridView ID =GridView1runat =server
OnRowDataBound =GridView1_RowDataBoundAutoGenerateColumns =false>
<列>
< asp:TemplateField HeaderText =String Value>
< ItemTemplate>
<%#Container.DataItem%>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>
在您的代码隐藏中:
protected void Page_Load(object sender,EventArgs e){
string [] arrayOfStrings = new string [] {first,second,third};
GridView1.DataSource = arrayOfStrings;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e){
if(e.RowType == DataControlRowType.DataRow){
// e。 Row的类型为GridViewRow
// e.Row.DataItem包含被绑定的原始值,
//但它是对象类型的,因此您需要将其转换为字符串。
字符串值=(字符串)e.Row.DataItem;
$ b $ p
$ b 解决此问题的唯一合理解决方法是创建一个包含属性的包装类。或者,如果您使用的是.NET 3.5,则可以使用LINQ创建一个临时列表,其中只包含您的值作为类的属性。有一个的例子此技巧在MSDN论坛上 vtcoder 。
列表< string> names = new List< string>(new string [] {John,Frank,Bob});
var bindableNames =从名字中的名字
选择new {Names = name};
GridView1.DataSource = bindableNames.ToList();
然后Name就是DataKeyName和BoundField DataField。
Can anyone give me a short snippet that, given a GridView that was originally bound to a simple string array, and given a GridViewRow row, will return the value that was bound to the row?
解决方案 You can't, the only property System.String has is Length, and DataKeyName expects a property of the object you are binding to. To answer your second question, here is an example of getting the string value from a GridViewRow.
In your ASPX file:
<asp:GridView ID="GridView1" runat="server"
OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="String Value">
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In your codebehind:
protected void Page_Load(object sender, EventArgs e) {
string[] arrayOfStrings = new string[] { "first", "second", "third" };
GridView1.DataSource = arrayOfStrings;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
// e.Row is of type GridViewRow
// e.Row.DataItem contains the original value that was bound,
// but it is of type object so you'll need to cast it to a string.
string value = (string)e.Row.DataItem;
}
}
The only reasonable workaround to the problem is to either create a wrapper class that has properties. Or if you are using .NET 3.5, you can use LINQ to make a temporary list which only includes your value as a property of the class. There is an example of this technique on MSDN Forums by vtcoder.
List<string> names = new List<string>(new string[] { "John", "Frank", "Bob" });
var bindableNames = from name in names
select new {Names=name};
GridView1.DataSource = bindableNames.ToList();
Then "Name" would be the DataKeyName and BoundField DataField.
这篇关于什么是绑定到字符串数组的gridview的dataKeyName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!