Sorting editing and deleting with the GridView

Sorting editing and deleting with the GridView

The GridView control in ASP.NET provides built-in support for sorting, editing, and deleting data. Here is how you can implement these features:

Sorting:

Set the GridView control’s AllowSorting property to true.

Set the SortExpression property for each column that you want to be sortable.

Handle the Sorting event of the GridView control and set the SortDirection property of the event argument to Ascending or Descending, depending on the user’s selection.

Editing:

Set the GridView control’s AutoGenerateEditButton property to true.

Handle the RowEditing event of the GridView control to set the EditIndex property to the index of the row being edited.

Handle the RowCancelingEdit event of the GridView control to cancel the editing operation.

Handle the RowUpdating event of the GridView control to update the data in the underlying data source.

Deleting:

Set the GridView control’s AutoGenerateDeleteButton property to true.

Handle the RowDeleting event of the GridView control to delete the data from the underlying data source.

Here is some sample code that demonstrates how to implement sorting, editing, and deleting with the GridView control:

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” AllowSorting=”True” AutoGenerateEditButton=”True” AutoGenerateDeleteButton=”True” OnSorting=”GridView1_Sorting” OnRowEditing=”GridView1_RowEditing” OnRowCancelingEdit=”GridView1_RowCancelingEdit” OnRowUpdating=”GridView1_RowUpdating” OnRowDeleting=”GridView1_RowDeleting”>

    <Columns>

        <asp:BoundField DataField=”ID” HeaderText=”ID” SortExpression=”ID” />

        <asp:BoundField DataField=”Name” HeaderText=”Name” SortExpression=”Name” />

        <asp:BoundField DataField=”Email” HeaderText=”Email” SortExpression=”Email” />

    </Columns>

</asp:GridView>

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

{

    // Set the sort direction based on the current sort direction

    if (e.SortDirection == SortDirection.Ascending)

    {

        e.SortDirection = SortDirection.Descending;

    }

    else

    {

        e.SortDirection = SortDirection.Ascending;

    }

    // Bind the data to the GridView control

    BindData();

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

    // Set the edit index to the index of the row being edited

    GridView1.EditIndex = e.NewEditIndex;

    // Bind the data to the GridView control

    BindData();

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

    // Cancel the editing operation

    GridView1.EditIndex = -1;

    // Bind the data to the GridView control

    BindData();

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

    // Update the data in the underlying data source

    // …

    // Cancel the editing operation

    GridView1.EditIndex = -1;

    // Bind the data to the GridView control

    BindData();

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

    // Delete the data from the underlying data source

    // …

    // Bind the data to the GridView control

    BindData();

}

private void BindData()

{

    // Bind the data to the GridView control

    // …

}

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Share this post
[social_warfare]
Enhancing the GridView Control
Comparison Operators

Get industry recognized certification – Contact us

keyboard_arrow_up