[英]Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource
[英]GridView, It must be either an IListSource, IEnumerable, or IDataSource
這是我的源代碼
<asp:GridView ID="gveducationInfo" CssClass="footable" runat="server" AutoGenerateColumns="false" Style="max-width: 600px" ShowHeaderWhenEmpty="true" DataKeyNames="Education_ID" OnRowCommand="gveducationInfo_RowCommand">
<Columns>
<asp:BoundField DataField="Education_ID" HeaderText="" ItemStyle-CssClass="hiddenColumn" HeaderStyle-CssClass="hiddenColumn" />
<asp:BoundField DataField="Degree_Type" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Year_Of_Passing" HeaderText="Year Of Passing" />
<asp:BoundField DataField="Institute_Name" HeaderText="Institute Name" />
<asp:BoundField DataField="State_ID" HeaderText="State" />
<asp:BoundField DataField="City_ID" HeaderText="City" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<%--<asp:BoundField DataField="Notes" HeaderText="Notes" />--%>
<asp:ButtonField ButtonType="Image" ImageUrl="~/images/edit16.png" CommandName="EditRow" ItemStyle-CssClass="align-center" />
<asp:ButtonField ButtonType="Image" ImageUrl="~/images/delete16.png" CommandName="DeleteRow" ItemStyle-CssClass="align-center" />
</Columns>
</asp:GridView>
這是我的C#編碼
protected void butSave_ServerClick(object sender, EventArgs e)
{
Business.ATS.ATS ats = new Business.ATS.ATS();
Data.Education_Info education_Info = new Data.Education_Info();
var state = ats.GetStates();
var city = ats.GetCities();
education_Info = ats.GetEducationInfo(Applicant_ID).SingleOrDefault();
education_Info = new Data.Education_Info();
education_Info.Education_ID = 0;
education_Info.Applicant_ID = Applicant_ID;
education_Info.Name = txtdegreename.Value;
education_Info.Year_Of_Passing = txtyearofpassing.Value;
education_Info.Institute_Name = txtnameofinstitution.Value;
education_Info.City_ID = Convert.ToInt32(ddlcity.SelectedValue);
education_Info.State_ID = Convert.ToInt32(ddlstate.SelectedValue);
education_Info.Degree_Type = Convert.ToInt32(ddldegreetype.SelectedValue);
ats.SaveEducationalInfo(education_Info);
gveducationInfo.DataSource = ats.GetEduInfo().Last();
gveducationInfo.DataBind();
}
這是我的實體框架數據庫代碼
public IList<Education_Info> GetEduInfo()
{
using (Data.ATSEntities dc = new Data.ATSEntities())
{
var lsteducationinfo = dc.Education_Info.ToList();
return lsteducationinfo;
}
}
當我填寫網頁中的詳細信息並單擊保存頁面時,我需要獲取用戶在gridview中輸入的最后一條記錄,請輸入錯誤
數據源是無效類型。 它必須是IListSource,IEnumerable或IDataSource。
有誰知道如何騎它?
這里調用Last()
是個問題:
gveducationInfo.DataSource = ats.GetEduInfo().Last();
刪除對Last()
的調用,如下所示:
gveducationInfo.DataSource = ats.GetEduInfo();
為什么?
GetEduInfo
方法返回IList
, IList
實現IEnumerable
,它將與DataSource
。 但是,當你調用Last()
時,你不再傳遞IEnumerable
,而是傳遞序列的最后一項,這將無法工作。
如果要將單個項綁定到DataSource
,則需要將其作為IEnumerable
包裝起來。 您可以使用任何答案將單個項目傳遞為IEnumerable <T> ,例如將單個項目包裝在數組中:
gveducationInfo.DataSource = new Education_Info[] {ats.GetEduInfo().Last()};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.