1

i have and grid view which i set an templatefield in it. in addition i set a datasource dynamically in code behind. now the templatefield appears as the first column, and i want to move it to be the 5 one, and also disable another column. how can i do it ?

             <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>

Aviadjo
  • 635
  • 5
  • 17
  • 36

3 Answers3

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
k.chinni66
  • 43
  • 8
0

Add 4 empty template fields before Username and set thier css to disabled:disabled using CssClass property of ItemStyle tag within TemplateField tag.

            <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>
Software Engineer
  • 3,906
  • 1
  • 26
  • 35
0

Try this:

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());
        }
Vinod
  • 4,672
  • 4
  • 19
  • 26
  • the explanation there do not tell how to move templatefield column to be between database fields, only before or after. – Aviadjo Mar 20 '12 at 07:49