2011-07-07 5 views
6

मैं एक GridView मिल गया है:क्या एएसपी को बांधना संभव है: सूची <T> पर ग्रिड व्यू?

<asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False"> 
<Columns> 
    <asp:BoundField DataField="JobNumber" HeaderText="Job" /> 
    <asp:BoundField DataField="ContainerType" HeaderText="Type" /> 
    <asp:BoundField DataField="ReleaseDate" HeaderText="Date" /> 
    <asp:BoundField DataField="Commodity" HeaderText="Commodity" /> 
    <asp:BoundField DataField="GrossWeight" HeaderText="Weight" /> 
    <asp:BoundField DataField="SpecialInstructions" HeaderText="Special Instructions" /> 
</Columns> 
</asp:GridView> 

मैं होने के लिए डेटा स्रोत सेट करने के लिए कोशिश कर रहा हूँ कि एक List<Restitution>() जहां Restitution एक सार्वजनिक केवल सार्वजनिक सदस्यों से मिलकर संरचना है, अर्थात्:

public struct Restitution 
{ 
    public int ContainerReleasesId; 
    public int ContainerId; 
    public System.DateTime ReleaseDate; 
    public int DepotId; 
    public string DepotName; 
    public string JobNumber; 
    public string BillOfLadingNumber; 
    public string BookingType; 
    public string Commodity; 
    public string SpecialInstructions; 
    public int GrossWeight; 
    public bool Confirmed; 
    public bool RecievedFlag; 
    public bool ReleaseSource; 
    public int ContainerTypeId; 
    public string InOut; 
    public string ContainerTypeDescription; 
} 

डेटा बाइंडिंग दिखता काफी अहानिकर, भी:

grdRestitutions.DataSource = restitutions; 
grdRestitutions.DataBind(); 

हालाँकि, एक अपवाद की तुलना में कम उपयोगी संदेश के साथ DataBind() बयान पर फेंक दिया जाता है:

" 'जॉब नम्बर' नाम वाला फ़ील्ड या प्रॉपर्टी चयनित डेटा स्रोत पर नहीं मिली थी। "

मुझे समझ में नहीं आता कि यह क्यों काम नहीं कर रहा है; जबकि अधिकांश उदाहरण और उपयोग के मामले DataSet एस का उपयोग करते हैं, ऐसा लगता है कि इसे IEnumerable लागू करने वाली वस्तुओं का समर्थन करना चाहिए। क्या मुझे कुछ काम करने की अनुमति देने के लिए कुछ खास करना है?

+0

आपका कोड मेरे लिए सही दिखता है। क्या आप सुनिश्चित हैं कि सही डेटा स्रोत के साथ बाध्यकारी है और यह शून्य या कुछ भी नहीं है। – Magnus

उत्तर

11

सार्वजनिक संपत्तियों के लिए सभी सार्वजनिक क्षेत्रों को कनवर्ट करें और इसे काम करना चाहिए।

public struct ContainerRelease 
{ 
    public int ContainerReleasesId {get; set;} 
    public int ContainerId {get; set;} 
    public System.DateTime ReleaseDate {get; set;} 
    ... 
} 
+0

लेकिन अपवाद कहता है "एक क्षेत्र या संपत्ति" – Magnus

+0

@Magnus आह, सच! मैंने हमेशा गुणों का उपयोग किया है। क्या आप गुणों को एक शॉट दे सकते हैं? अगर यह काम नहीं करता है, तो मैं अपना जवाब हटा दूंगा। –

+1

@ मैग्नस ऐसा लगता है जैसे आप "अपवाद कहता है" ट्रस्ट के साथ-साथ मुझे विश्वास भी है। यह पता चला है कि यह सदस्यों (यहां तक ​​कि सार्वजनिक लोगों) से डेटा बाध्यकारी का समर्थन करने के लिए * प्रकट नहीं होता है, केवल गुण ... –

1

इस प्रयास करें:

<asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False"> 
<Columns> 
    <asp:templatefield headertext="Job"> 
    <itemtemplate> 
      <asp:label id="JobNumberLabel" 
      Text="<%# ((Restitution)Container.DataItem).JobNumber %>" 
      runat="server"/> 
    </itemtemplate> 
    </asp:templatefield> 
</Columns> 
</asp:GridView> 

इस कोड, एक Restitution वस्तु के लिए प्रत्येक के लिए बाध्य पंक्ति डाले तो JobNumber क्षेत्र सीधे एक्सेस करता है। यदि यह काम करता है, तो आप अन्य क्षेत्रों को समान रूप से बांध सकते हैं। यदि यह काम नहीं करता है, तो त्रुटि आपको वास्तविक समस्या तक ले जाती है।

1

हां, आपको इस मामले में सूची को बांधने में सक्षम होना चाहिए।

<asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False"> 
<Columns> 
    <asp:TemplateField HeaderText="Job" > 
    <ItemTemplate> 
     <%# DataBinder.Eval(Container, "DataItem.Job") %> 
    </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 
</asp:GridView> 

जांची नहीं लेकिन यदि आप किसी ऐसे जाना देना चाहिए:

<asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False"> 
<Columns> 
    <asp:BoundField DataField="DataItem.JobNumber" HeaderText="Job" /> 
    <asp:BoundField DataField="DataItem.ContainerType" HeaderText="Type" /> 
    <asp:BoundField DataField="DataItem.ReleaseDate" HeaderText="Date" /> 
    <asp:BoundField DataField="DataItem.Commodity" HeaderText="Commodity" /> 
    <asp:BoundField DataField="DataItem.GrossWeight" HeaderText="Weight" /> 
    <asp:BoundField DataField="DataItem.SpecialInstructions" HeaderText="Special Instructions" /> 
</Columns> 
</asp:GridView> 

कि आप संभवतः इसलिए जैसे TemplateColumns का उपयोग कर की कोशिश कर सकते काम नहीं करता है: बात तो आप इस तरह के और अधिक कुछ करने की ज़रूरत है ...

संबंधित मुद्दे