Asp.net gridview pagination and rowcommand issue - c#

I have a gridview with page size= 10 and onrowcommand to button cliked event in gridview.
<asp:GridView runat="server" ID="gvCourseAssignments" AutoGenerateColumns="false" AllowPaging="true" ShowFooter="false" OnRowCommand="gvCourseAssignments_RowCommand" OnRowDataBound="gvCourseAssignments_RowDataBound" OnPageIndexChanged="gvCourseAssignments_PageIndexChanged" OnPageIndexChanging="gvCourseAssignments_PageIndexChanging" **PageSize="10"** Width="100%">
But in second page the gridview only have 2 rows.but when i clicked the button or rowcommand fired the gridview getting filled with empty rows to adjust the page size =10. but in second page has only 2 rows.
Please help.

protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
FillGrid();
grdView.PageIndex = e.NewPageIndex;
grdView.DataBind();
}

protected void gvCourseAssignments_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCourseAssignments.PageIndex = e.NewPageIndex;
bindGridview();
gvCourseAssignments1.DataBind();
}
If this is done, kindly update your question with your .cs code.

Related

LinkButton.Click event not firing when dynamically attached in GridView row databound

I have a asp:GridView inside of an asp:UpdatePanel, which has a column of asp:LinkButton controls.
On the row databound event, the LinkButton gets it's click event handler assigned.
I've tried every way I could find to wire up the click even and none of the events ever fire.
Am I doing something wrong?
aspx:
<asp:UpdatePanel ID="MainUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTest" Text="test" runat="server" />
<asp:GridView ID="gvClientsArchive" runat="server" AllowSorting="true" DataSourceID="dsClients"
OnRowDataBound="gvClientsArchive_RowDataBound" SkinID="gvList"
AllowPaging="true" PageSize="25" Visible="false">
...
Code behind:
protected void gvClientsArchive_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
int company_id = int.Parse(drvRow["company_id"].ToString());
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += new System.EventHandler(this.doRestore);
Button handler code:
private void doRestore(object sender, EventArgs e)
{
lblTest.Text = "restore clicked";
}
I've also tried:
protected void gvClientsArchive_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += delegate
{
lblTest.Text = "restore clicked";
};
RowDataBound is inappropriate if you want to register event handlers. Use RowCreated:
protected void gvClientsArchive_RowCreated(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += new System.EventHandler(this.doRestore);
}
}
RowDataBound is triggered only if you Databind the grid not on every postback which is needed since all controls are disposed at the end of the page's lifecycle. It's also too late.
If you use TemplateFields it's easier to register the handler declaratively on the aspx.

error The GridView 'GridView1' fired event Sorting which wasn't handled

I am trying to Create two groups In Data Grid like this example http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm
but there are an error and I don't know what is the reason or how to solve it
Sorting event seems not handled properly here.
You can handle it in following manner
<asp:GridView ID="GridView1" runat="server" AllowSorting="true"
OnSorting="GridView1_Sorting">
</asp:GridView>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
dataTable.DefaultView.Sort = e.SortExpression ;
GridView1.DataSource = dataTable;
GridView1.DataBind();
}
In the gridview sorting event bind your gridview again
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridView1.DataBind();
}

GridView with DataTable as source not autopaging or sorting

Basically I'm setting my datasource on my gridview to a datatable that I filled in manually. Is there a way to get the gridview to autopage and sort? When I try to go to another page on the gridview it gives me an exception saying that the paging event is not being caught (meaning I don't have a method for the event). I have allow paging set to true as well.
you have to implement OnPageIndexChanging event for paging , OnSorting event for sorting like :
<asp:GridView ID="GridView1" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" runat="server" />
and in code behind :
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// sort expression
}
you can refer to this Example

The GridView fired event PageIndexChanging which wasn't handled

i have allowed paging and added the below codes but got the error. Does anyone know what could be the problem?
Code:
protected void SubmitAppraisalGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
SubmitAppraisalGrid.PageIndex = e.NewSelectedIndex;
SubmitAppraisalGrid.DataBind();
}
Design:
<asp:GridView ID="SubmitAppraisalGrid" runat="server"
AutoGenerateColumns="False" BorderWidth="0px"
onrowcreated="SubmitAppraisalGrid_RowCreated" ShowHeader="False"
style="margin-right: 0px" AllowPaging="True" PageSize="1"
onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging">
</asp:GridView>
If you have set a gridviews AllowPaging attribute to “true” and do not handle the PageIndexChanging event then this error raise.
To work with paging add the PageIndexChanging event handler to grid and change your markup and code as:
<asp:GridView ID="SubmitAppraisalGrid" runat="server"
AutoGenerateColumns="False" BorderWidth="0px"
onrowcreated="SubmitAppraisalGrid_RowCreated" ShowHeader="False"
style="margin-right: 0px" AllowPaging="True" PageSize="1"
onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging"
OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging">
</asp:GridView>
///
protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
SubmitAppraisalGrid.DataBind();
//bindGrid();
//SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
//SubmitAppraisalGrid.DataBind();
}
protected void SubmitAppraisalGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
/// you selected index related logic here.
}
This event is not raised when you programmatically set the PageIndex property.
Check MSDN documentation of GridView.PageIndexChanging Event
For reference:
The GridView fired event PageIndexChanging which wasn't handled
Your code should be inside On PageIndexChanging Event
protected void SubmitAppraisalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
SubmitAppraisalGrid.DataBind();
}
Design:
<asp:GridView ID="SubmitAppraisalGrid" runat="server"
AutoGenerateColumns="False" BorderWidth="0px"
onrowcreated="SubmitAppraisalGrid_RowCreated" ShowHeader="False"
style="margin-right: 0px" AllowPaging="True" PageSize="1"
OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging">
</asp:GridView>
try
OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging"
instead of
onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging"
protected void SubmitAppraisalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
BindGrid();
}
insted of using
SubmitAppraisalGrid.PageIndex = e.NewSelectedIndex;
you must use
SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
and if you got error again plese post the error too..
Step by Step:
Select gridview from design and go to property and fire the event (PageIndexChanging)
Code : gridviewname.pageindex=e.NewPageIndex;
You need to call the Pageindex changing event from selected index changing event of dropdown.
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
// Retrieve the pager row.
GridViewRow pagerRow = SubmitAppraisalGrid.BottomPagerRow;
// Retrieve the PageDropDownList DropDownList from the bottom pager row.
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
// Set the PageIndex property to display that page selected by the user.
GridViewPageEventArgs evt = new GridViewPageEventArgs(pageList.SelectedIndex);
SubmitAppraisalGrid_PageIndexChanging(sender, evt);
}

GridView Paging is not working?

I have a grid in that i am using this code on page init
UpagedList = new PagedListAdapter<User>(UserListGridView);
UpagedList.MaxRows = ConfigurationService.DefaultPageSize;
UserListGridView.PageIndexChanged += delegate
{
Presenter.FillDataOnDropDown();
};
UserListGridView.Sorting += new GridViewSortEventHandler(UserListGridView_Sorting);
UserListGridView.Sorted += delegate {Presenter.SortChanged(); };
my grid code:
<asp:GridView CssClass="Greed" ID="UserListGridView" runat="server" DataSourceID="ListUserDataSource"
AutoGenerateColumns="false" EmptyDataText="No data found" DataKeyNames="Id" OnSorting="UserListGridView_Sorting" AllowSorting="True"
PageSize="25" AllowPaging="True" GridLines="None" EnableViewState="false">
<Columns></Columns>
</asp:GridView>
Paging is not working...? what else i need to do for paging.. when i am clicking on 2nd page page is not getting change but data is getting appended in grid
I think you missing the OnPageIndexChanging event in the gridview. Try adding this to your gridview OnPageIndexChanging="UserListGridViewIndexChanging"
and in the backend code
protected void UserListGridViewIndexChanging(object sender, GridViewPageEventArgs e)
{
UserListGridView.PageIndex = e.NewPageIndex;
Bind(); // you data bind code
}
hope this helps
protected void UserListGridViewIndexChanging(object sender, GridViewPageEventArgs e)
{
UserListGridView.PageIndex = e.NewPageIndex;
UserListGridView.DataBind();
Bind(); // you data bind code is here
}

Categories