这是我的源代码
<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。
任何人都知道如何驾驭它?
答案 0 :(得分:0)
此处对Last()
的调用是问题所在:
gveducationInfo.DataSource = ats.GetEduInfo().Last();
删除对此Last()
的调用:
gveducationInfo.DataSource = ats.GetEduInfo();
为什么呢?
GetEduInfo
方法返回IList
,IList
实现IEnumerable
,它将与DataSource
一起使用。但是,当您调用Last()
时,您不再传递IEnumerable
,而是传递序列的最后一项,这将无效。
如果要将单个项目绑定到DataSource
,则需要将其包装为IEnumerable
。您可以使用此Passing a single item as IEnumerable<T>的任何答案,例如将单个项目包装在数组中:
gveducationInfo.DataSource = new Education_Info[] {ats.GetEduInfo().Last()};