在gridview中更改templatefield列位置

时间:2012-03-20 07:19:52

标签: asp.net gridview templatefield

我有和网格视图,我在其中设置了一个模板字段。 另外,我在代码后面动态设置数据源。 现在模板字段显示为第一列,我想将其移动为5,并禁用另一列。 我该怎么做 ?

             <asp:GridView ID="gv_DisAlarms" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" PageSize="15"
            AllowSorting="True" OnSorting="dataGrid_Sorting" 
            EnableModelValidation="True" AutoGenerateColumns="true">
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <Columns> 
                <asp:TemplateField HeaderText="Username" HeaderStyle-HorizontalAlign="Left" >
                    <ItemTemplate>
                        <asp:HyperLink ID="hl_mailto" runat="server"  Text='<%# Bind("Username") %>' NavigateUrl='<%# "mailto:" + Eval("Username") + "?subject=Disabled Alarms&body=Dear " + Eval("Username") + ".%0AIn " + Eval("LastEditDate") + " you put alarm " + Eval("PointID") + " in disable.%0APlease clarify the reason and when it will be enabled.%0AThanks."  %>'  />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView>

<asp:GridView ID="gv_DisAlarms" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" PageSize="15" AllowSorting="True" OnSorting="dataGrid_Sorting" EnableModelValidation="True" AutoGenerateColumns="true"> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <Columns> <asp:TemplateField HeaderText="Username" HeaderStyle-HorizontalAlign="Left" > <ItemTemplate> <asp:HyperLink ID="hl_mailto" runat="server" Text='<%# Bind("Username") %>' NavigateUrl='<%# "mailto:" + Eval("Username") + "?subject=Disabled Alarms&body=Dear " + Eval("Username") + ".%0AIn " + Eval("LastEditDate") + " you put alarm " + Eval("PointID") + " in disable.%0APlease clarify the reason and when it will be enabled.%0AThanks." %>' /> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> </asp:GridView>

3 个答案:

答案 0 :(得分:1)

    Protected Sub gvEdit_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvEdit.RowCreated
    Dim row As GridViewRow = e.Row
    Dim columns As New List(Of TableCell)()

    For Each column As DataControlField In gvEdit.Columns
        'Get the first Cell /Column
        Dim cell As TableCell = row.Cells(1)
        ' Then Remove it after
        row.Cells.Remove(cell)
        'And Add it to the List Collections
        columns.Add(cell)
    Next

    ' Add cells
    row.Cells.AddRange(columns.ToArray())

End Sub

答案 1 :(得分:0)

在Username之前添加4个空模板字段,并将其css设置为disabled:使用TemplateField标记内的ItemStyle标记的CssClass属性禁用。

            <Columns> 
            <asp:TemplateField HeaderText="Username" HeaderStyle-HorizontalAlign="Left" >
                <ItemStyle CssClass="colDisable" />
                <ItemTemplate>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Username" HeaderStyle-HorizontalAlign="Left" >
                <ItemStyle CssClass="colDisable" />
                <ItemTemplate>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Username" HeaderStyle-HorizontalAlign="Left" >
                <ItemStyle CssClass="colDisable" />
                <ItemTemplate>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Username" HeaderStyle-HorizontalAlign="Left" >
                <ItemStyle CssClass="colDisable" />
                <ItemTemplate>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

答案 2 :(得分:0)

试试这个:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
        GridViewRow row = e.Row;
        List<TableCell> columns = new List<TableCell>();
        foreach (DataControlField column in GridView1.Columns)
        {
            TableCell cell = row.Cells[0];
            row.Cells.Remove(cell);
            columns.Add(cell);
        }
        row.Cells.AddRange(columns.ToArray());
        }