如何在ASP.NET中修改gridview中模板字段的列索引
我有一个如何在ASP.NET中修改gridview中模板字段的列索引,asp.net,vb.net,gridview,templatefield,Asp.net,Vb.net,Gridview,Templatefield,我有一个网格视图,其中包含自动生成的列和模板字段。 我正在使用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", Ge
网格视图
,其中包含自动生成的列
和模板字段
。
我正在使用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()
行创建的代码
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的列
使用该方法创建列的副本。然后清除GridView的列集合,并按所需顺序重新添加列
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的字段时,我只得到与模板字段列计数相等的计数。因此,我得到错误:“索引超出范围。索引必须是非负整数”