这是我的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 最终成为第一列,但我希望在自动生成的列之后。
答案 0 :(得分:2)
在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);
}
答案 1 :(得分:0)
您将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>
答案 2 :(得分:0)
恐怕可能无法实现。阅读MS documentation:
您还可以将显式声明的列字段与 自动生成的列字段。两者都明确使用时 声明的列字段首先呈现,然后是 自动生成的列字段。自动生成绑定 列字段未添加到Columns集合。