DataGridView DataSource C# Source Code

How to fill DataGridView items on c# if we’re connect using OleDbConnection?

We’re gonna use DataSet for filling the DataGridView.. Here’is the example…

The information about variables and objects used is

  1. dataGridView1 –> DataGridView GUI Object from Toolbox
  2. Program.con –> OleDbConnection (using System.Data.OleDb)
  3. combo_produk_id –> ComboBox GUI Object from Toolbox
        private void Item_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            DataSet ds = new DataSet();

            Program.con.Open();
            
            OleDbCommand cmd = Program.con.CreateCommand();
            cmd.CommandText = "SELECT * FROM produk";

            OleDbDataReader reader = cmd.ExecuteReader();
            combo_produk_id.Items.Clear();
            while (reader.Read())
            {
                combo_produk_id.Items.Add(reader.GetValue(0));
            }

            Program.con.Close();

            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
            adapter.Fill(ds);
            
            dataGridView1.DataSource = ds.Tables[0];
            
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation