English 中文(简体)
使用列表用于 网格视图 数据源时, 我如何在列表中设置列名?
原标题:When using list for gridview s datasource, how do I set the column name on the list?
  • 时间:2012-05-23 15:54:25
  •  标签:
  • c#
  • asp.net

基本上我有一个网格视图,它使用列表作为数据源。我如何告诉网格视图列表中的哪个列是哪个?

        // Regions
        Dictionary<int, string> regions = (Master as SiteMaster).getCustomerRegions((Master as SiteMaster).userBasic_XsiteCustomerID);
        List<List<string>> regionsList = new List<List<string>>();
        foreach (int curRegionID in regions.Keys)
        {
            List<string> curRegion = new List<string>();
            Dictionary<string, string> curRegionDetail = (Master as SiteMaster).getRegionDetail(curRegionID);
            curRegion.Add(curRegionID.ToString());
            curRegion.Add(curRegionDetail["regionName"]);
            regionsList.Add(curRegion);
        }
        grv_regionManagement.DataSource = regionsList;
        grv_regionManagement.DataBind();

因此,它给了我一个错误, 说区域ID没有定义, 这很有意义, 因为没有区域ID告诉 网格观点,第一列实际上就是区域ID。

以下是ASP.NET中的代码,用于定义区域ID字段:

            <asp:TemplateField HeaderText="regionID" InsertVisible="False" 
                SortExpression="regionID" Visible="False">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text= <%# Bind("regionID") %> ></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text= <%# Bind("regionID") %> ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
最佳回答

与其列出字符串列表,不如列出含有列属性的对象列表。您可以将这些属性用作每列的数据源。

问题回答

如果您愿意放弃列表, 请从列表中组成一个数据表, 然后指定数据表为数据源 。 参见 < a href=" https:// stackoverflow. com/ questions/10588329_how- to- set- a- gridview- cloumns- text- deput- on- other- columns- what- ar- binded/ 10588405# 10588405# SO post 并修改它以从列表中填充数据表 。

这样,你不必担心 告诉你的电网视图什么。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签