Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3693236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:21:15+00:00 2026-05-19T04:21:15+00:00

I have a DataGridView on my Form which is being populated with database records

  • 0

I have a DataGridView on my Form which is being populated with database records on the click event button.
How can I populate another two column template programatically at run time?

The two column template are Qty In Stock and Status
This is my datagridview what looks like when populated from database on click event button…

===============================================================
FoodName        FoodType     Qty In Stock     Status
===============================================================
Olives          Starter                     
Soup            Starter                     
Caprese     Starter
Bruschetta     Starter
Mushroom     Starter
Antipasto     Starter
Scallops     Starter
Calamari     Starter
Crab Avocado    Starter
Pizza Bread     Starter
===============================================================

And this is the datagridview what I want to look like populating the other two columns at the run-time

=================================================================
FoodName        FoodType     Qty In Stock     Status
=================================================================
Olives          Starter      0                Allways On Stock
Soup            Starter      0                Allways On Stock
Caprese     Starter      0                Allways On Stock
Bruschetta     Starter      0                Allways On Stock
Mushroom     Starter      0                Allways On Stock
Antipasto     Starter      0                Allways On Stock
Scallops     Starter      0                Allways On Stock
Calamari     Starter      0                Allways On Stock
Crab Avocado    Starter      0                Allways On Stock
Pizza Bread     Starter      0                Allways On Stock
=================================================================

Here is the code generating the datagridview from database on click event button…

private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
        private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();

        private void cmdStarters_Click(object sender, EventArgs e)
        {
            OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
            connBuilder.DataSource = @"C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposMenu.accdb";
            connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
            connBuilder.Add("Jet OLEDB:Engine Type", "5");

            // Food SQL Query
            string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
            using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
            {
                dataGridView1.Columns.Clear();
                dataGridView1.RowTemplate.Height = 60;
                //====================================\\
                dataGridView1.Visible = true;
                dataGridView2.Visible = false;
                try
                {
                    OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
                    OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
                    OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
                    //DataRow dr;
                    DataSet ds = new DataSet();
                    conn.Open();
                    foodType.Value = "Starter";
                    foodsDa.Fill(ds, "Food_table");

                    conn.Close();
                    dataGridView1.DataSource = ds;
                    dataGridView1.DataMember = "Food_table";

                    dataGridView1.Columns.AddRange(ColFoodQtyStock, ColFoodStatus);

                    DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
                    this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
                    dataGridViewCellStyle1.Font = new Font("Verdana", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                    this.dataGridView1.Columns[0].Width = 420;
                    this.dataGridView1.Columns[1].Width = 180;
                    this.dataGridView1.Columns[2].Width = 300;
                    this.dataGridView1.Columns[3].Width = 308;

                    // ColStatus 
                    ColFoodStatus.HeaderText = "Status";
                    ColFoodStatus.Name = "ColFoodStatus";

                    // ColQtyStock
                    ColFoodQtyStock.HeaderText = "Quantity In Stock";
                    ColFoodQtyStock.Name = "ColFoodQtyStock";

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex);
                }
            }
        }

Could someone help me to modify the code on my click event button to solve this problem please

Thanks in advance

lapeci

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:21:15+00:00Added an answer on May 19, 2026 at 4:21 am

    if you want columns added at runtime (although Qty in Stock sounds like a database column) then the easiest way is to add them directly to your dataset.

        ...    
        foodsDa.Fill(ds, "Food_table");
    
        //add extra column structures to dataset
        ds.Tables["Food_table"].Columns.Add(new DataColumn("ColFoodQtyStock", System.Type.GetType("System.Int32")));
        ds.Tables["Food_table"].Columns.Add(new DataColumn("ColFoodStatus", System.Type.GetType("System.String")));
        //loop through all the rows and add the data to the new columns dynamically
        for (int i = 0; i < ds.Tables["Food_table"].Rows.Count; i++)
        {
            ds.Tables["Food_table"].Rows[i]["ColFoodQtyStock"] = 0;
            ds.Tables["Food_table"].Rows[i]["ColFoodStatus"] = "Always On Stock";
        }
    
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "Food_table";
    
        DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
        dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
        dataGridViewCellStyle1.Font = new Font("Verdana", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    
        dataGridView1.Columns[0].Width = 420;
        dataGridView1.Columns[1].Width = 180;
        dataGridView1.Columns[2].Width = 300;
        dataGridView1.Columns[3].Width = 308;
    }
    catch (Exception ex)
    ...
    

    PS: you don’t need to call .Open() or Close() on a connection when using a dataAdapter as it is done automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagridview where the users can select which subset of columns to
I have a datagridview which im binding DataTable to. What I want do is
How can I have a datagridview that will autogenerate a textbox instead of a
Say I have a DataGridView in which I dynamically build up a ComboBox column
I have a DataGridView with one DataGridViewComboBoxColumn in my WinForms application. I need to
I have a DataGridView with its datasource set to a generic list of custom
I have a DataGridView that I want to query using Linq (C# WinForm). I
I have a DataGridView control in a winforms app that I'm working on. The
I have a DataGridView binded to this table: [Users] ID Name -- ---- 11
In my application I have a DataGridView control that displays data for the selected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.