Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts

Sunday, August 17, 2008

How to Solve this error "The GridView 'MyGridview' fired event PageIndexChanging which wasn't handled."

You can see this error when you are populating a Gridview with code, and you enabled the Paging Property of the Gridview without implementing the PageIndexChanging Event.

So when you click the next Page in the Gridview you will see this error:

"The GridView 'MyGridview' fired event PageIndexChanging which wasn't handled."

To avoid this issue simply write this code in the PageIndexChanging Event:
MyGridview.PageIndex = e.NewPageIndex;
MyGridview.DataBind();

Saturday, August 9, 2008

Confirmation Messagebox when deleting an item from the GridView ASP.NET Control

Many Users click the delete button within the GridView with no attention then the result the item will be deleted from the GridView, so we can add confirmation messagebox to appear to tell the user "Are you sure you want to delete this item?".

First of all we have to attach the javascript code for each delete button within the GridView, we can do that in the itemdatabound method of the GridView:
HttpContext.Current.Response.Write(MyString);
LinkButton deleteButton = e.Item.Cells[0].Controls[0];
deleteButton.Attributes.Add("onclick","javascript:return confirm('Are you sure you want to delete this item?')");
where the first cell in the GridView is a linkbutton.
So when the user clicks the delete button then a confirmation message appears so if the user clicks No then nothing happens else postback will happen then it should be a server event such as a onDeleteCommand that fires when the user clicks the delete button as
private void dgPendingCompleteForms_Delete(Object sender, DataGridCommandEventArgs e)
{
    //Put your code here when the user presses the yes button on the confirmation meessagebox
}


Sunday, August 3, 2008

Change the back color of a Gridview row when you click the Edit button or link

If you have an editable Gridview and you need to change the backcolor of the row you are editing, just write these tow lines of code in the Gridview's RowEditing Event.
MyGridview.SelectedIndex = e.NewEditIndex;
MyGridviewID.SelectedRow.BackColor = Drawing.Color.Red;

Tuesday, April 15, 2008

How to Clear GridView Quickly

If you need to completely clear a GridView quickly, write the following code:
protected void ClearGrid(GridView gv)
{
    gv.DataSource = null;
    gv.DataSourceID = null;
    gv.DataBind();
}