This is my 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>
</as开发者_如何学Gop:GridView>
The TestColumn ends up being the first column, but i want it after the auto generated ones.
In the RowDataBound
event handler, you can move the TemplateField cell from the first column to the end of the row:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
TableCell cell = e.Row.Cells[0];
e.Row.Cells.RemoveAt(0);
e.Row.Cells.Add(cell);
}
I am afraid it may not be possible. Read MS documentation:
You can also combine explicitly declared column fields with automatically generated column fields. When both are used, explicitly declared column fields are rendered first, followed by the automatically generated column fields. Automatically generated bound column fields are not added to the Columns collection.
You set the AutoGenerateColumnProperty to false, and then you order your columns as you like.
If you just want to add an edit button, you should use:
<asp:CommandField ShowEditButton="True" />
Here is an example using the northwind database
<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>
精彩评论