簡體   English   中英

如何在自動生成的列之后在gridview中插入列-ASP.NET

[英]How to insert a column in a gridview after the auto generated columns - ASP.NET

這是我的gridview:

<asp:GridView ID="gridview" runat="server" AutoGenerateColumns="true">
    <Columns>
        <asp:TemplateField HeaderText="TestColumn">
            <ItemTemplate>
                <asp:LinkButton ID="lkbtn" runat="server" Text="Edit"
                    CommandName="Update" CausesValidation="False" ToolTip="Edit" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

TestColumn最終成為第一列,但在自動生成的列之后我想要它。

RowDataBound事件處理程序中,可以將TemplateField單元格從第一列移動到行尾:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    TableCell cell = e.Row.Cells[0];
    e.Row.Cells.RemoveAt(0);
    e.Row.Cells.Add(cell);
}

恐怕不可能。 閱讀MS文檔

您還可以將顯式聲明的列字段與自動生成的列字段結合使用。 當同時使用這兩個字段時,將首先呈現顯式聲明的列字段,然后是自動生成的列字段。 自動生成的綁定列字段不會添加到Columns集合中。

您將AutoGenerateColumnProperty設置為false,然后根據需要對列進行排序。

如果只想添加一個編輯按鈕,則應使用:

<asp:CommandField ShowEditButton="True" />

這是使用northwind數據庫的示例

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ProductID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"/>
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"/>
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"/>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>

暫無
暫無

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

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