none
bind dataGridView datasource with IEnumerable<> RRS feed

  • Question

  • I got confused since everyone says datagridbiew cannot be bound with IEnumareble<>, but I made it. maybe we are not on the same page? I am a .net beginner BTW. could anyone give me a hint? Thx.

    part of my code, it works fine.

        public class DataOperator
        {
           public IEnumerable<student_info> Refresh()
           {
               LSCDataContext db = new LSCDataContext(); //Linqtosql class
               var dt = from p in db.student_infos where p.Age>10 select p;
               return dt;
           }
        }

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

             private void btnRefresh_Click(object sender, EventArgs e)
            {
                DataOperator dot = new DataOperator();
                dataGridView1.DataSource = dot.Refresh();
            }
        }

    • Moved by Mike FengModerator Friday, July 20, 2012 8:06 AM Linq (From:.NET Base Class Library)
    Thursday, July 19, 2012 7:11 PM

Answers

  • Hi WeiRdNet,

    Welcome to MSDN Forum.

    LINQ to SQL implements IListSource in two locations:

    • The data source is a Table<TEntity>: LINQ to SQL browses the table to fill a DataBindingList collection that keeps a reference on the table.

    • The data source is an IQueryable<T>. There are two scenarios:

      • If LINQ to SQL finds the underlying Table<TEntity> from the IQueryable<T>, the source allows for edition and the situation is the same as in the first bullet point.

      • If LINQ to SQL cannot find the underlying Table<TEntity>, the source does not allow for edition (for example, groupby). LINQ to SQL browses the query to fill a generic SortableBindingList, which is a simple BindingList<T> that implements the sorting feature for T entities for a given property.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 20, 2012 8:12 AM
    Moderator

All replies

  • Hi WeiRdNet,

    Welcome to the MSDN Forum.

    I have moved this thread to Linq forum for better support.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 20, 2012 8:09 AM
    Moderator
  • Hi WeiRdNet,

    Welcome to MSDN Forum.

    LINQ to SQL implements IListSource in two locations:

    • The data source is a Table<TEntity>: LINQ to SQL browses the table to fill a DataBindingList collection that keeps a reference on the table.

    • The data source is an IQueryable<T>. There are two scenarios:

      • If LINQ to SQL finds the underlying Table<TEntity> from the IQueryable<T>, the source allows for edition and the situation is the same as in the first bullet point.

      • If LINQ to SQL cannot find the underlying Table<TEntity>, the source does not allow for edition (for example, groupby). LINQ to SQL browses the query to fill a generic SortableBindingList, which is a simple BindingList<T> that implements the sorting feature for T entities for a given property.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 20, 2012 8:12 AM
    Moderator