簡體   English   中英

將按鈕放在gridview中的最后一列

[英]Place button as LAST column in gridview

我有一個網格視圖,我通過代碼添加列:

//Retrieve "Table" from database
gridOffers.DataSource = table;
gridOffers.DataBind();

按代碼添加的列是在按鈕之后添加的,這不是我需要的: 在此輸入圖像描述 如何確保添加到購物車按鈕最后一件事?

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。

相反,為什么不將GridView的AutoGenerateColumns設置為False ,然后使用BoundField為GridView源中的數據源所需的每個字段顯示指定列順序,然后是圖像和按鈕列?

我不知道是否有更少的hacky方式,但您可以使用RowDataBound來更改順序,以便AutoGenerated列跟隨您的其他列:

protected void gridOffers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    TableCell cell = e.Row.Cells[0];
    e.Row.Cells.RemoveAt(0);
    //Move first to the end
    e.Row.Cells.Add(cell);
    cell = e.Row.Cells[0];
    e.Row.Cells.RemoveAt(0);
    //Move second to the end
    e.Row.Cells.Add(cell);
}

您是否在Gridview上將AutoGenerateColumns設置為true? 如果是這樣,則將其設置為false並使用asp:BoundField手動設置網格中的字段將DataField設置為數據源中字段的名稱。

這樣您就可以設置列的確切順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM