gridView 绑定string[]数组,列名的问题

ihuidx 2011-08-14 05:39:24
CS源码
string[] str = System.Web.Security.Roles.GetAllRoles();
this.gridview.datasource=str;
this.gridview.databind();


aspx 文件

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="角色" DataField="这里绑定的名字填什么" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" CommandName="del" Text="删除" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
`
</EmptyDataTemplate>
</asp:GridView>


<asp:BoundField HeaderText="角色" DataField="这里绑定的名字填什么" />


我想要我效果

角色 操作
管理员 button
会员 button
系统管理员 button
...全文
310 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
暖枫无敌 2011-08-14
  • 打赏
  • 举报
回复
DataItem 属性标识在数据绑定操作和后期绑定表达式中所使用的对象。

参考MSDN
http://msdn.microsoft.com/zh-cn/library/system.web.ui.idataitemcontainer.dataitem.aspx
暖枫无敌 2011-08-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ihuidx 的回复:]

引用 10 楼 taomanman 的回复:
C# code

<asp:TemplateField HeaderText="角色">
<ItemTemplate>
<%#Container.DataItem%>
</ItemTemplate>
</asp:TemplateField>


这个可以。什么原理
[/Quote]
灵活的运用数据绑定操作
绑定到简单属性:<%#UserName%>
绑定到集合:<asp:ListBox id="ListBox1" datasource='<%# myArray%>' runat="server">
绑定到表达式:<%#(class1.property1.ToString() + "," + class1.property2.ToString())%>
绑定到方法返回值:<%# GetSafestring(str) %>
绑定到Hashtable:<%# ((DictionaryEntry)Container.DataItem).Key%>
绑定到ArrayList:<%#Container.DataItem %>

若数组里里放的是对象则可能要进行必要的转换后再绑定如:
<%#((对象类型)Container.DataItem).属性%>

绑定到DataView,DataTable,DataSet:
<%#((DataRowView)Container.DataItem)["字段名"]%>或
<%#((DataRowView)Container.DataItem).Rows[0]["字段名"]%>
要格式化则:
<%#string.Format("格式",((DataRowView)Container.DataItem)["字段名"])%>
<%#DataBinder.Eval(Container.DataItem,"字段名","格式")%>

绑定到DataReader:
<%#((IDataReader)Container.DataItem).字段名%>

当然为了方便一般使用最多的就是DataBinder类的Eval方法了.不过这样对于同时要绑定大量的数据效率要低一些

在绑定数据时经常会用到这个句程序:<%# DataBinder.Eval(Container.DataItem,"xxxx")%>或者<%# DataBinder.Eval(Container,"DataItem.xxxx")%>

今天又学到一种,而且微软也说这种方法的效率要比以上两种高。

<%# ((DataRowView)Container.DataItem)["xxxx"]%>

很有用的,这样可以在前台页面做好多事情了。

还要记住要这样用必须要在前台页面导入名称空间System.Data,否则会生成错误信息。

<%@ Import namespace="System.Data" %>

这种用法其实和<%# ((DictionaryEntry)Container.DataItem).Key%>是一个道理。

Text='<%# DataBinder.Eval(Container.DataItem, "字段") %>' 这样的方法是最快的

Text='<%# GetPrice() %>' 也可以绑定方法,但方法要是public的

Text='<%# "CarDetails.aspx?CarID=" + DataBinder.Eval(Container.DataItem, "CarID") %>' 还可以连接多个字段

关键是Container这个东西,它比较神秘。它的名称空间是System.ComponentModel。

初学.NET,现在在看DataGrid控件,在ItemTemplate显示数据时,

DataBinder.Eval(Container.DataItem,"Name")和Container.DataItem("Name")有什么区别?DataBinder是

System.Web里面的一个静态类,它提供了Eval方法用于简化数据绑定表达式的编写,但是它使用的方式是通过Reflection等开销比

较大的方法来达到易用性,因此其性能并不是最好的。而Container则根本不是任何一个静态的对象或方法,它是ASP.NET页面编译

器在数据绑定事件处理程序内部声明的局部变量,其类型是可以进行数据绑定的控件的数据容器类型(如在Repeater内部的数据绑定

容器叫RepeaterItem),在这些容器类中基本都有DataItem属性,因此你可以写Container.DataItem,这个属性返回的是你正在

被绑定的数据源中的那个数据项。如果你的数据源是DataTable,则这个数据项的类型实际是DataRowView。
  • 打赏
  • 举报
回复



你的 GetAllRoles(); 这个方法是数据层的方法吗?

如果是数据层的东西的话

你检查一下你的SQL语句.

不然你的 datasource 绑定的数据从哪里来呢?
ihuidx 2011-08-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 taomanman 的回复:]
C# code

<asp:TemplateField HeaderText="角色">
<ItemTemplate>
<%#Container.DataItem%>
</ItemTemplate>
</asp:TemplateField>
[/Quote]

这个可以。什么原理
暖枫无敌 2011-08-14
  • 打赏
  • 举报
回复

<asp:TemplateField HeaderText="角色">
<ItemTemplate>
<%#Container.DataItem%>
</ItemTemplate>
</asp:TemplateField>
暖枫无敌 2011-08-14
  • 打赏
  • 举报
回复
是数组的话,这样试试看,注意红色部分、

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="角色">
<ItemTemplate>
<%#Container.DataItem%>
</ItemTemplate>
</asp:TemplateField>

<asp:Button ID="btn" CommandName="del" Text="删除" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:GridView>

ihuidx 2011-08-14
  • 打赏
  • 举报
回复
如果不指定。系统自动生成的列。列名是item,但我手工绑定item没有用
暖枫无敌 2011-08-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ihuidx 的回复:]

string[] str = new string[]{“管理员","系统管理员","会员"}

就是string 数组呀
[/Quote]

看你的那个方法那么专业,还以为你操作的是实体类呢?我晕
ihuidx 2011-08-14
  • 打赏
  • 举报
回复
string[] str = new string[]{“管理员","系统管理员","会员"}

就是string 数组呀
LMAOhuaNL 2011-08-14
  • 打赏
  • 举报
回复
我知道请问你GetAllRoles();这个方法里面肯定有获取的字段啥
暖枫无敌 2011-08-14
  • 打赏
  • 举报
回复
你的这个System.Web.Security.Roles.GetAllRoles();方法应该返回一个实体类的集合吧???
如:
以下代码假设你的实体类为Roles,角色字段为RoleName:


List<Roles> roles = System.Web.Security.Roles.GetAllRoles();
this.gridview.DataSource=roles;
this.gridview.DataBind();

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="角色">
<ItemTemplate>
<%# ((Roles)Container.DataItem).RoleName %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" CommandName="del" Text="删除" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:GridView>






LMAOhuaNL 2011-08-14
  • 打赏
  • 举报
回复
<asp:TemplateField HeaderText="角色">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("角色字段") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn" CommandName="del" Text="删除" runat="server" />
</EditItemTemplate>
</asp:TemplateField>

你这样也可以呀
ihuidx 2011-08-14
  • 打赏
  • 举报
回复
数据源是一个 strin[] 数组,
LMAOhuaNL 2011-08-14
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="角色" DataField="很明显这里就填你的需要绑定的角色字段" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" CommandName="del" Text="删除" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
`
</EmptyDataTemplate>
</asp:GridView>




110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧