问题描述
我有一个网格视图,并通过代码向其中添加了列:
I have a grid view and I add columns to it by code:
//Retrieve "Table" from database
gridOffers.DataSource = table;
gridOffers.DataBind();
代码添加的列是在按钮之后添加的,这不是我需要的:我如何确保添加到购物车"按钮是最后一件事?
The columns added by code are being added AFTER the button, which is not what I need:How do I make sure the Add To Cart button the very last thing?
gridView 的来源:
Source of gridView:
<Columns>
<asp:ImageField DataImageUrlField="ImageUrl" ControlStyle-Width="100"
ControlStyle-Height = "100" HeaderText = "Preview Image"
ItemStyle-HorizontalAlign="Center">
<ControlStyle Height="100px" Width="100px"></ControlStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ImageField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false"
CommandName="btnAddToCart" Text="Add To Cart" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
推荐答案
您正在 GridView 源中定义列,并且通过将数据源数据绑定到 GridView.因此,您有两个列来源;可能有也可能没有保证将列添加到 GridView 的顺序.
You're defining columns in the GridView source AND by databinding a data source to the GridView. Therefore you have two sources of columns; there may or may not be a guaranteed order in which columns are added to the GridView.
相反,为什么不将 GridView 的 AutoGenerateColumns 设置为 False,然后使用 BoundField 为您想要的每个字段显式指定列顺序来自 GridView 源中的数据源,然后是图像和按钮列?
Instead, why not set AutoGenerateColumns for the GridView to False and then explicitly specify the column order by using BoundField for each field you want from the data source in your GridView source followed by your image and button columns?
这篇关于将按钮作为 gridview 中的最后一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!