Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

GridView DataSource και Custom Object

Îåêßíçóå áðü ôï ìÝëïò gspiros. Τελευταία δημοσίευση από το μέλος KelMan στις 28-04-2009, 17:44. Υπάρχουν 3 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  28-04-2009, 14:28 50308

    GridView DataSource και Custom Object

    Λοιπόν, έχω το:
    public class Class1
    {
        public string name;
        public int ID;
       
        public static int count = 0;
       
        public Class1()
            : this("test_", count)
        {
            count++;
        }
        public Class1(string n, int i)
        {
            this.name = n + count.ToString();
            this.ID = i;
        }
    }
    απλά μια class, για παράδειγμα.

    Μπορώ να κάνω populate ένα gridview, βάση αυτής της class?
    Eγώ κάνω το εξής:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Class1 c1 = new Class1();
            Class1 c2 = new Class1();
            Class1 c3 = new Class1();

            IList<Class1> list = new List<Class1>();
            list.Add(c1);
            list.Add(c2);
            list.Add(c3);

            Dictionary<int, Class1> dict = new Dictionary<int, Class1>();
            dict.Add(1, c1);
            dict.Add(2, c2);
            dict.Add(3, c3);

            //GridView1.DataSource = list;
            GridView1.DataSource = dict.Values;
            GridView1.DataBind();
        }
    }
    έχω δοκιμάσει και το Dictionary και το List

                <asp:GridView AutoGenerateColumns="false" ID="GridView1" runat="server">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" />
                        <asp:BoundField DataField="name" HeaderText="Name" />
                    </Columns>
                </asp:GridView>

    Έχει κανείς καμιά ιδέα πως θα δουλέψει?

    Σκάει HttpException
    A field or property with the name 'ID' was not found on the selected data source.

    Όταν θέσω AutoGenerateColumns="true" σκάει πάλι HttpException.
    The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns.  Ensure that your data source has content.

    Και τα 2 σκάνε κατά το DataBind().
  •  28-04-2009, 16:18 50310 σε απάντηση της 50308

    Απ: GridView DataSource και Custom Object

    Φτιάξε τα public  fields ως κανονικά properties με setters και getters.


    Vir prudens non contra ventum mingit
  •  28-04-2009, 16:41 50311 σε απάντηση της 50310

    Απ: GridView DataSource και Custom Object

    Σε ευχαριστώ, έτσι όντως δουλεύει!
    Αλλά γιατί?
  •  28-04-2009, 17:44 50317 σε απάντηση της 50311

    Απ: GridView DataSource και Custom Object

    O data binding μηχανισμός έχει φτιαχτεί έτσι ώστε να απαιτεί properties. Αυτό γίνεται γιατί αφενός το data binding δουλεύει με τους λεγόμενους PropertyDescriptors (βλ ICustomTypeDescriptor) και όχι με reflection και αφετέρου γιατί μέσω των properties υποστηρίζεται η υλοποίηση interfaces όπως INotifyPropertyChanged, το IEditableObject, κλπ

     


    Vir prudens non contra ventum mingit
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems