ASP.NET 如何修改 gridview 中模板字段的列索引
How to modify column index of a template field in a gridview in ASP.NET
我有一个 gridview
,同时包含 autogenerated columns
和 template field
。
我正在使用 GridView.RowCreated
将我的模板字段移到最右侧。但是,我需要做的是将它放在 HOUR
列和 NAME
列之间。
是否可以在自动生成的列之间插入模板字段列?
VB.NET
Dim dt As New DataTable()
dt.Columns.Add("Date", GetType(String))
dt.Columns.Add("Hour", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Status", GetType(String))
'fill table
GridView1.DataSource = dt
GridView1.DataBind()
RowCreated 代码
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim row As GridViewRow = e.Row
' Intitialize TableCell list
Dim columns As New List(Of TableCell)()
For Each column As DataControlField In GridView1.Columns
'Get the first Cell /Column
Dim cell As TableCell = row.Cells(0)
' Then Remove it after
row.Cells.Remove(cell)
'And Add it to the List Collections
columns.Add(cell)
Next
row.Cells.AddRange(columns.ToArray())
End Sub
ASPX
<asp:GridView ID="GridView1" runat="server" CssClass="Grid">
<RowStyle Font-Bold="False" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Control" ShowHeader="True" Visible="True"
ItemStyle-HorizontalAlign="Center">
<HeaderTemplate> View </HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="B_SHOW" runat="server" ImageUrl="~/MA/Images and Icons/zoom-icon.png" Width="22px" Height="22px" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' CommandName="EXECUTE_QUERY"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
您可以按任何顺序排列 GridView 的列。
使用 CloneFields()
方法创建您的专栏的副本。之后清除 GridView 的 Column 集合并按要求的顺序重新添加 Columns。
Dim columnsCollection As DataControlFieldCollection =
GridView1.Columns.CloneFields()
GridView1.Columns.Clear()
' Now add the Columns one by one in any order
' add 3rd column as the FIRST Column
GridView1.Columns.Add(columnsCollection(3))
' Add 7th Column as Second Column
GridView1.Columns.Add(columnsCollection(7))
你也可以使用Insert()
方法,如果需要的话::
GridView1.Columns.Insert(0, columnsCollection(3))
我有一个 gridview
,同时包含 autogenerated columns
和 template field
。
我正在使用 GridView.RowCreated
将我的模板字段移到最右侧。但是,我需要做的是将它放在 HOUR
列和 NAME
列之间。
是否可以在自动生成的列之间插入模板字段列?
VB.NET
Dim dt As New DataTable()
dt.Columns.Add("Date", GetType(String))
dt.Columns.Add("Hour", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Status", GetType(String))
'fill table
GridView1.DataSource = dt
GridView1.DataBind()
RowCreated 代码
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim row As GridViewRow = e.Row
' Intitialize TableCell list
Dim columns As New List(Of TableCell)()
For Each column As DataControlField In GridView1.Columns
'Get the first Cell /Column
Dim cell As TableCell = row.Cells(0)
' Then Remove it after
row.Cells.Remove(cell)
'And Add it to the List Collections
columns.Add(cell)
Next
row.Cells.AddRange(columns.ToArray())
End Sub
ASPX
<asp:GridView ID="GridView1" runat="server" CssClass="Grid">
<RowStyle Font-Bold="False" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Control" ShowHeader="True" Visible="True"
ItemStyle-HorizontalAlign="Center">
<HeaderTemplate> View </HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="B_SHOW" runat="server" ImageUrl="~/MA/Images and Icons/zoom-icon.png" Width="22px" Height="22px" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' CommandName="EXECUTE_QUERY"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
您可以按任何顺序排列 GridView 的列。
使用 CloneFields()
方法创建您的专栏的副本。之后清除 GridView 的 Column 集合并按要求的顺序重新添加 Columns。
Dim columnsCollection As DataControlFieldCollection =
GridView1.Columns.CloneFields()
GridView1.Columns.Clear()
' Now add the Columns one by one in any order
' add 3rd column as the FIRST Column
GridView1.Columns.Add(columnsCollection(3))
' Add 7th Column as Second Column
GridView1.Columns.Add(columnsCollection(7))
你也可以使用Insert()
方法,如果需要的话::
GridView1.Columns.Insert(0, columnsCollection(3))