I'm having a little bit of trouble sorting out the paging scenario with a gridview i.e. I can't get the bloody thing to show page, 2, 3, 4, etc.
I have the following Grid view code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
style="z-index: 1; left: 20px; top: 440px; position: absolute; height: 133px; "
AllowPaging="True" AllowSorting="True" Font-Size="Small"
PageSize="2" onpageindexchanging="Gr开发者_如何转开发idView1_PageIndexChanging">
<Columns>
With the following
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
Now, I am getting a "TargetinvocationException was unhandled by user code."
Being a newbie, this is beyond my current capabilities and has confused me somewhat. How do I go about binding my gridview properly to allow for the paging to be operate correctly?
this is where things get interesting! I am using a linq data source:
<asp:LinqDataSource ID="**lqPackWeights**" runat="server"
ContextTypeName="ORWeightsDataClassesDataContext"
Select="new (UnitId, UnitDescription, PackagingTypeCode, UnitWeight, WeightUnitCode, RecycledContent, IsBiodegradable, Recyclability, RevisionSourceCode, RevisionDate, ExtendedMaterialName, MaterialText, WeightStatus, ProductPercentage, UnitUserfield1, UnitUserfield2, IDDesc, MaterialLevel)"
TableName="tblOnlineReportingCOMPLETEWeights" Where="IDDesc == @IDDesc">
</asp:LinqDataSource>
lqPackWeights being generated through this:
private object GetMaterialData(string MemberKey, string MaterialType, string MaterialLevel, int Count)
{
ORWeightsDataClassesDataContext db = new ORWeightsDataClassesDataContext();
var query = db.tblOnlineReportingCOMPLETEWeights
.Where(x => x.MaterialLevel == MaterialLevel && x.MaterialText == MaterialType && x.MemberId == MemberKey)
.OrderByDescending(x => x.ProductPercentage).Take(Count);
return query;
}
protected void btSearch_Click(object sender, EventArgs e) {
lqPackWeights.WhereParameters.Clear();
ControlParameter cp = new ControlParameter();
cp.Type = TypeCode.String;
if (radBuyer.Checked)
{
cp.ControlID = "ddlProd";
cp.PropertyName = "SelectedValue";
cp.Name = "IDDesc";
lqPackWeights.WhereParameters.Add(cp);
GridView1.DataSourceID = "lqPackWeights";
GridView1.DataBind();
}
else if (radProd.Checked)
{
cp.ControlID = "tbxProdAC";
cp.PropertyName = "Text";
cp.Name = "IDDesc";
lqPackWeights.WhereParameters.Add(cp);
GridView1.DataSourceID = "lqPackWeights";
GridView1.DataBind();
}
I suspect I have one too many binds kicking about...as you can probalby tell from my code, this is new territory for me so be gentle with the abuse!
Ok, I've read a few places that I need to thrown in the data source so I have now got the following code
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSourceID = "lqPackWeights";
}
Now when I build the page, it works fine, but when I hit page 2 of x, I receive this nasty little message:
Server Error in '/onlineReportingFUNCTIONING' Application.
This provider supports Skip() only over ordered queries returning entities or projections that contain all identity columns, where the query is a single-table (non-join) query, or is a Distinct, Except, Intersect, or Union (not Concat) operation.
What's that about then?!
The binding was actually fine. This was finally sorted by adding in a primary key (which I really should have implmented in the first place!)
So if anyone is reading this, cannot get their gridview to page through an objectdatasource, make sure you have a primary key!!!
精彩评论