Thursday, April 23, 2020

How to display data in datagridview from datatable 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_Datatable : Form
    {
        public frmFillDataGridView_With_Datatable()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ID Card", typeof(string));
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Father Name", typeof(string));
                dt.Columns.Add("Phone", typeof(string));
                dt.Columns.Add("Address", typeof(string));
                dt.Rows.Add("23344-1234567-7", "John Doe", "Elma, Rocks", "(+1)-123-333333", "Street 1, Isb, Pak");
                dt.Rows.Add("23344-1234567-8", "John Albert", "Rocky, Hun", "(+92)-199-3110003", "Street 2, Wah Cantt, Pak");
                dt.Rows.Add("23344-1234567-9", "John Elsi", "Jimmy, Adams", "(+97)-777-5422433", "Street 3, Rwp, Pak");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {

            }
        }
    }

No comments:

Post a Comment

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     {...