2016-09-27 59 views
1

這是我的源代碼GridView控件,它必須是一個IListSource,IEnumerable的,或者的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(); 
} 

這是我的實體framwork DB代碼

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

Last()這裏調用的問題是:

gveducationInfo.DataSource = ats.GetEduInfo().Last(); 

刪除調用Last()這樣的:

gveducationInfo.DataSource = ats.GetEduInfo(); 

爲什麼?

GetEduInfo該方法返回一個IListIList實現IEnumerable,將與DataSource工作。但是,當您調用Last()時,您不再傳遞IEnumerable,而是傳遞序列的最後一項,這不起作用。

如果要將單個項目綁定到DataSource,則需要將其包裝爲IEnumerable。你可以使用任何的答案這個Passing a single item as IEnumerable<T>,諸如陣列中的包裹的單個項目:

gveducationInfo.DataSource = new Education_Info[] {ats.GetEduInfo().Last()}; 
+0

我刪除了最後(),但我在網頁電網得到了全表,但我只需要最後一個記錄該用戶已進入。那可能嗎?? @Shaun Luttin –

+0

@KarthikeyanMuthukumaran是的。這是可能的。查看修改。 –

+0

非常感謝你@Shaun Luttin –

相關問題