Change the position of a Commandfield in gridview

Asked

Viewed 47 times

2

I’m a beginner and I’m trying to create a dynamic table that can be edited and deleted certain fields so I decided to use gridview. The problem I cannot solve is the position of this commandfield that I created to delete, it is always on the left and the rest of the table is fed dynamically by the database.

Useredit.aspx

<tbody id="tb" runat="server">
    <asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" CssClass="table table-bordered">
        <Columns>
            <asp:CommandField ItemStyle-HorizontalAlign="Right" ItemStyle-Width="20" HeaderStyle-HorizontalAlign="Right"  DeleteText="<img src='imagens/recyclebin-512.png'" ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>
</tbody>

Useredit.aspx.Cs

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = _sql.GetEditUtilizadores();
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

Output: inserir a descrição da imagem aqui

  • 1

    To leave it on the right, I think you should use the Boundfields for each field.

  • Could you explain it a little better? What’s the difference?

  • 1

    Is in answer with code example ;)

1 answer

2


Within your TAG of Columns add the Boundfields, are fields already predefined in the code, with this you lose a little of the dynamism of the Grid, but I think it is one of the outputs, and I could think of.

Ex.:

<Columns>
        <asp:BoundField Visible="False" DataField="Id" HeaderText="ID" />
        <asp:BoundField DataField="Nome" HeaderText="NOME" />
        <asp:CommandField ItemStyle-HorizontalAlign="Right" ItemStyle-Width="20" HeaderStyle-HorizontalAlign="Right"  DeleteText="<img src='imagens/recyclebin-512.png'" ShowDeleteButton="True" />
</Columns>

Bounce that on the property DataField of BoundField you must put the same name as your property within your class or table, or create collections.

You already leave in the code which fields your grid will accept, so later on codebehind define how Visible false the fields which you do not want to appear at any given time. Ex.:

GridView1.Columns[0].Visible = false;

I hope I helped, it was the only way I could think.

  • 1

    Thank you so much Augusto, I finally understand why I use boundfields.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.