Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a ComboBoxColumn in my datagridview. This is populated from database with a list of items. I need another column with price from my items table on the database. When a user selects an item from the Items Combobox, I need the price to be displayed on the column next to it.

What I have tried:

This is how I have populated the items column

DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            cmb.DisplayIndex = 0;
            cmb.HeaderText = "Item";
            cmb.Name = "cmb";

            SQLiteConnection conn = ConnectonManager.GetConnection();
            string query = "SELECT item_id, item_name  FROM items;";
            SQLiteDataAdapter da = new SQLiteDataAdapter(query, conn);
            conn.Open();
            DataSet ds = new DataSet();
            da.Fill(ds, "items");
            cmb.DisplayMember = "item_name";
            cmb.ValueMember = "item_id";
            cmb.DataSource = ds.Tables["items"];


            dataGridView1.Columns.Add(cmb);


How do I dynamically fill the item price column?
Posted

1 solution

try this

using System;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            LoadData();
            dataGridView1.EditingControlShowing +=dataGridView1_EditingControlShowing;
        }

        private void LoadData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");

            dataGridView1.DataSource = dt;

            //ADD COMBOBOX COLUMN
            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            combo.HeaderText = "Item Name";
            combo.Name = "combo";
            var dat = new String[] { "Item 1", "Item 2", "Item 3", "Item 4" };
            combo.Items.AddRange(dat);

            //ADD THE COLUMN  TO DATAGRIDVIEW
            dataGridView1.Columns.Add(combo);
            dt.Columns.Add("Price");
        }
        ComboBox combo;
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //GET OUR COMBO OBJECT
            combo = e.Control as ComboBox;
            if (combo != null)
            {
                // AVOID ATTACHMENT TO MULTIPLE EVENT HANDLERS
                combo.SelectedIndexChanged -= new EventHandler(combo_SelectedIndexChanged);

                //THEN NOW ADD
                combo.SelectedIndexChanged += combo_SelectedIndexChanged;
            }
        }

        private void combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = (sender as ComboBox).SelectedItem.ToString();
            double price  =50;
            var target = sender as  DataGridViewComboBoxEditingControl;
                int rowIndex = target.EditingControlRowIndex;
                if (value == "Item 1")
                { price = 100; }
                if (value == "Item 2")
                { price = 200; }

                  dataGridView1.Rows[rowIndex].Cells["Price"].Value = price; 
        }
    }
}


modified from C# DataGridView ComboBoxColumn Selection Event - Camposha[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900