将按钮在GridView的最后一列 [英] Place button as LAST column in gridview
问题描述
我有一个网格视图,我用code列添加到它:
//从数据库中检索表
gridOffers.DataSource =表;
gridOffers.DataBind();
由code添加的列被添加该按钮后,这是不是我所需要的:
我如何确保放入购物车按钮的最后的事情吗?
GridView控件来源:
<柱体和GT;
< ASP:ImageField的DataImageUrlField =的ImageUrlControlStyle-WIDTH =100
ControlStyle-HEIGHT =100的HeaderText =preVIEW图像
ItemStyle-HorizontalAlign =中心>
&所述; ControlStyle高度=100px的宽度=100像素>&下; / ControlStyle>< ItemStyle HorizontalAlign =中心>< / ItemStyle>
< / ASP:ImageField的>
< ASP:的TemplateField ShowHeader =FALSE>
<&ItemTemplate中GT;
< ASP:按钮的ID =Button1的=服务器的CausesValidation =假
的CommandName =btnAddToCart文本=放入购物车/>
< / ItemTemplate中>
< / ASP:的TemplateField>
< /专栏>
您正在定义的列在GridView来源和数据绑定的数据源到GridView。因此,必须列有两个来源;有可能或不可能是其中列添加到GridView保证顺序。
相反,为什么不将的AutoGenerateColumns 为GridView为假,然后用的BoundField 作为您希望每个领域明确指定列顺序从后跟您的形象和按钮列你的GridView源中的数据源?
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?
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>
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.
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的最后一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!