Showing posts with label display data in datagridview using list. Show all posts
Showing posts with label display data in datagridview using list. Show all posts

Thursday, April 23, 2020

How to fill datagridview from List using c#



This code shows how we can display a datatable in the datagridview in our windows form application using c#.


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

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                List<User> lstUser = new List<User>();
                lstUser.Add(new User
                {
                    IDCardNo = "23344-1234567-7",
                    Name = "John Doe",
                    Phone = "(+1)-123-333333",
                    Address = "Street 1, Isb, Pak"
                });
                lstUser.Add(new User
                {
                    IDCardNo = "23344-1234567-7",
                    Name = "John Albert",
                    Phone = "(+92)-199-3110003",
                    Address = "Street 2, Wah Cantt, Pak"
                });
                lstUser.Add(new User
                {
                    IDCardNo = "23344-1234567-9",
                    Name = "John Elsi",
                    Phone = "(+97)-777-5422433",
                    Address = "Street 3, Rwp, Pak"
                });
                dataGridView1.DataSource = lstUser;
            }
            catch (Exception ex)
            {

            }
        }
    }

    public class User
    {
        public string IDCardNo { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }

    }


How to use combobox using c#

This code shows how we can use combobox in our winforms application using c#.     public partial class frmComboBox_Demo : Form     {...