Binding IEnumerable<T>

Bind contains an overload which takes an IEnumerable<T> with T being any .Net class and builds the data grid from that collection. The property names of the class become the column names. If there are alias set on the column, that will be used as the column header's. Example:

FIrst add a datagridview control to the form and set matching column header names:

The bind sample data below:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DGV.Framework;

namespace csDemo
{
    public partial class Form1 : Form
    {
        public class User
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Gender { get; set; }
        }

        public Form1()
        {
            InitializeComponent();
        }
 

        private void Form1_Load(object sender, EventArgs e)
        {
            //create sample the records
            List<User> usersList = new List<User>();
            for (int i = 0; i < 10; i++)
            {
                usersList.Add(new User
                {
                    Age = 28,
                    Name = "Kim Too FLex",
                    Gender = "Male"
                });
            }

            //Bind the list to datagrid view
            _dataGridView.Bind(usersList);
        }
    }
}

Output:

Last updated

Was this helpful?