How to move auto create delete button on right side in gridview

i has the form of a grid with the following columns

Delete | Name | ContactNo | EmailID | CreateDate |

      

Delete column is auto-generated, I want to move it from the right side like

 | Name | ContactNo | EmailID | CreateDate | Delete

      

How can i do this?

+3


source to share


3 answers


The AutoGenerateDeleteButton won't show up at design time because VS will automatically add it at runtime, and as far as I know, this will be automatically added on the left side of the Grid screen and basically that's the default.

You can try adding the following next field to the projection view.



<asp:CommandField ShowDeleteButton="True" />

      

otherwise, you must create a button template column to delete.

+5


source


  • First of all you cannot do this with an auto generated delete button, so you have to set AutoGenerateDeleteButton="false"

  • Create a CommandField

    delete button for the button and place it below all the other columns so that it appears on the right:

ASPX:

<asp:GridView ID="gvEmployees" runat="server" AutoGenerateDeleteButton="false" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" />
        <asp:BoundField DataField="Name" />
        <asp:CommandField DeleteText="Delete" ShowDeleteButton="true" />
    </Columns>
</asp:GridView>

      



Code behind:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var employees = new List<Employee>{new Employee{Id="1",Name="Employee 1"}};
            gvEmployees.DataSource = employees;
            gvEmployees.DataBind();
        }
    }
}

public class Employee
{
    public string Id { get; set; }
    public string Name { get; set; }
}

      

+3


source


create a RowCreated Event on your grid... easy as that (this will swap the row to end,, no need to turn off auto generate... 100% worked for me)

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            // Intitialize TableCell list
            List<TableCell> columns = new List<TableCell>();
            foreach (DataControlField column in GridView1.Columns)
            {
                //Get the first Cell /Column
                TableCell cell = row.Cells[0];
                // Then Remove it after
                row.Cells.Remove(cell);
                //And Add it to the List Collections
                columns.Add(cell);
            }
            // Add cells
            row.Cells.AddRange(columns.ToArray());
        }

      

0


source







All Articles