GridView Paging is not working? - c#

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
}

Related

unable to update gridview values in .net

while updating gridview record . old values only getting updated. im using bound field. getting old value while im fetching data in variable.
<asp:GridView runat="server" ID="GvLeads" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" OnRowDeleting="GvLeads_RowDeleting" OnRowEditing="GvLeads_RowEditing" OnRowCancelingEdit="GvLeads_RowCancelingEdit" EmptyDataText="No Records Found" OnRowUpdating="GvLeads_RowUpdating" OnRowDeleted="GvLeads_RowDeleted">
<Columns>
<asp:BoundField HeaderText="Id" DataField="LeadId" />
<asp:BoundField HeaderText="Company" DataField="Companyname" />
</Columns>
</GridView >
code behind
protected void GvLeads_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GvLeads.Rows[e.RowIndex];
String str = ((TextBox)(row.Cells[1].Controls[0])).Text;
int Leadid = Convert.ToInt32(str);
string CompanyName = ((TextBox)(row.Cells[2].Controls[0])).Text;
}
This usually happens when you are populating grid at Page_Load as soon as RowUpdating event gets called before that Page_Load event get's called which populates the grid with initial values. How to Avoid? Use !IsPostBack for this purpose
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid(); // For e.g.
}
}

Asp.net gridview pagination and rowcommand issue

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.

gridview paging in popup

I am Working in ASP.NET and c#.
I have gridview which i am binding on a button click,also am showing this gridview in popup.Now paging is not working in my gridview ,please help me out to solve this problem.
Aspx:
<asp:GridView ID="GV_Order" runat="server" AllowPaging="true" AutoGenerateColumns="false" DataKeyNames="ProductCode" EmptyDataText="There are no data records to display."
BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px"
CellPadding="4" CellSpacing="2"
HeaderStyle-BackColor="Black" HeaderStyle-ForeColor="#FFD700" PageSize="5" OnPageIndexChanging="GV_Orderpageindexchanging" o>
codebehind:
protected void btn_click(object sender,eventargs e)
{
GV_Order.DataSource = dataset;
GV_Order.DataBind();
}
protected void GV_Orderpageindexchanging(object sender, GridViewPageEventArgs e)
{
GV_Order.PageIndex = e.NewPageIndex;
GV_Order.DataBind();
}
asp.net DataPager control is not usefull,use Jquery Data Table for this.its realy nice working for all table.
add 'datatable' class to which table you want to use 'paging', 'searching','ordering'.. plugins ,

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 and DetailsView Code Behind to Update DetailsView

I've spent all afternoon trying to do this using CODE BEHIND without success so I am asking for some C# code.
Basically, I have a GV and a DV in a master/detail relationship. GV displays ID and Name. If I click Select on a GV row, I want to see its ID, Name and Address in DV. I know how get this to work declaratively in an aspx file. But in C# code behind, I don't know how to proceed at this function:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Select"))
{
// PLEASE GIVE ME THE CODE HERE TO BIND THE DETAILSVIEW. THANKS!
// I am using a sqldatasource if it makes any difference
}
}
Here's a general solution showing you how to achieve this, please note that this solution isn't extremely error-safe but I suppose you'll get the jist of it. Please comment if there's anything unclear.
Code-behind:
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow selected = gv.Rows[Convert.ToInt32(e.CommandArgument)];
List<ThatClass> cList = new List<ThatClass>();
cList.Add(new ThatClass(selected.Cells[0].Text, selected.Cells[1].Text));
dv.DataSource = cList;
dv.DataBind();
}
}
Markup:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowCommand="gv_RowCommand">
<Columns>
<asp:BoundField DataField="A" HeaderText="A"/>
<asp:BoundField DataField="B" HeaderText="B" />
<asp:CommandField ShowSelectButton="true" />
</Columns>
</asp:GridView>
<asp:DetailsView runat="server" ID="dv">
</asp:DetailsView>
FYI: I bound the GV using a List:
protected void Page_Load(object sender, EventArgs e)
{
List<ThatClass> cList = new List<ThatClass>();
cList.Add(new ThatClass("123", "abc"));
cList.Add(new ThatClass("456", "def"));
gv.DataSource = cList;
gv.DataBind();
}

Categories