GridView中只显示第一个记录检索 [英] GridView displaying only first retrieved record
问题描述
我成功取回我从我的数据库所需要的所有记录,但只有一条记录是越来越显示在我的 GridView控件
。
我一直在试图解决这个在过去的几天,我因为我跑出来的方案,试图真的很感到沮丧。我迫切需要一些帮助。
我试图操纵查询,因为我本来认为这是问题,试图以我从
,尝试添加的IQueryable
检索数据转换清单<为MyObject>() .Cast<对象>()了ToList();
在的结束 GridView.DataSource
,甚至试图利用的ViewState的
S,以及更多!
请,任何人的帮助。
我有 onRowDataBound
和的SelectedIndexChanged
事件的 GridView控件
,因为我看过他们可以导致此问题。
下面是我的code:
GridView控件
gvSubCategories.DataSource =新BASubCategory()GetSubCatWithCatName()。
gvSubCategories.DataBind();
保护无效gvSubCategories_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
如果(e.Row.RowType == DataControlRowType.DataRow)
{
子类别currentSubCat =(子类别)e.Row.DataItem; //图片IMG =(图)e.Row.FindControl(imgSubCategory);
//img.ImageUrl = currentSubCat.ImageURL;
}
}保护无效gvSubCategories_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
BASubCategory baSubCat =新BASubCategory();
GUID ID =新的GUID(gvSubCategories.SelectedDataKey.Value.ToString());
子类别SUBCAT = baSubCat.GetSubCategory(ID); txtName.Text = subCat.Name;
imgImage.ImageUrl = subCat.ImageURL;
}
GridView控件定义
< DIV CLASS =内容> < ASP:GridView控件ID =gvSubCategories=服务器的DataKeyNames =ID的AutoGenerateColumns =假
onrowdatabound =gvSubCategories_RowDataBound
onselectedindexchanged =gvSubCategories_SelectedIndexChanged>
<柱体和GT;
< ASP:CommandField中ShowSelectButton =真/>
< ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:图片ID =imgSubCategory=服务器WIDTH =100px的高度=100px的/>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:BoundField的数据字段=类别名称的HeaderText =类别名称/>
< ASP:BoundField的数据字段=SubCategoryName的HeaderText =子类别名称/>
< ASP:BoundField的数据字段=SubCategoryImage的HeaderText =子类别图像/>
< /专栏>
< / ASP:GridView的>< / DIV>
我已经解决了这个问题喽。
由于 GridView控件
的的RowDataBound
事件声明,并且每行我的目标是由两种不同的实体
(子类别和组别),正被生成的错误
子类别currentSubCat =(子类别)e.Row.DataItem;
所提到的事件。它不能匿名对象(由两个子类别和类别)转换为子类别
,从而触发例外。这引起了 GridView控件
停止执行<$ C $ C>的RowDataBound 的第一个元素被放置后,从而只显示第一个元素。
我不得不删除上面的行,一切都完美。
I am successfully retrieving all the records I need from my database, however only one record is getting displayed in my GridView
.
I've been trying to solve this for the past couple of days, and I am really getting frustrated since I ran out of options to try. I am desperately in need of some help.
I've tried to manipulate the query because I originally thought that it was the problem, tried to convert the data I retrieve from IQueryable
to List<MyObject>()
, tried to add .Cast<object>().ToList();
at the end of the GridView.DataSource
, and even tried the use of ViewState
s, and much much more!
Please, anyone, help.
I do have onRowDataBound
and on selectedIndexChanged
events for the GridView
because I read that they can cause this issue.
Below is my code:
GridView:
gvSubCategories.DataSource = new BASubCategory().GetSubCatWithCatName();
gvSubCategories.DataBind();
protected void gvSubCategories_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SubCategory currentSubCat = (SubCategory)e.Row.DataItem;
//Image img = (Image)e.Row.FindControl("imgSubCategory");
//img.ImageUrl = currentSubCat.ImageURL;
}
}
protected void gvSubCategories_SelectedIndexChanged(object sender, EventArgs e)
{
BASubCategory baSubCat = new BASubCategory();
Guid id = new Guid(gvSubCategories.SelectedDataKey.Value.ToString());
SubCategory subCat = baSubCat.GetSubCategory(id);
txtName.Text = subCat.Name;
imgImage.ImageUrl = subCat.ImageURL;
}
GridView Definition
<div class="content">
<asp:GridView ID="gvSubCategories" runat="server" DataKeyNames="ID" AutoGenerateColumns="false"
onrowdatabound="gvSubCategories_RowDataBound"
onselectedindexchanged="gvSubCategories_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imgSubCategory" runat="server" Width="100px" Height="100px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CategoryName" HeaderText="Category Name" />
<asp:BoundField DataField="SubCategoryName" HeaderText="Sub Category Name" />
<asp:BoundField DataField="SubCategoryImage" HeaderText="Sub Category Image" />
</Columns>
</asp:GridView>
</div>
I've solved this problem myself.
Since the GridView
's RowDataBound
event was declared, and my object per row was made up of two different Entities
(SubCategory and Category), an error was being generated at the
SubCategory currentSubCat = (SubCategory)e.Row.DataItem;
of the mentioned event. It couldn't convert an Anonymous object (made up of both SubCategory and Category) to a SubCategory
, thus firing the exception. This caused the GridView
to stop executing the RowDataBound
after the first element was placed, thus displaying only the first element.
I had to remove the line above and everything worked perfectly.
这篇关于GridView中只显示第一个记录检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!