Home > DeveloperSection > Forums > How to Use ComboBox in DataGridView Windows Application c#
Ankit Singh

Total Post:341

Points:2389
Posted on    December-13-2015 10:13 PM

 C# C#  Windows Controls  Datagridview 
Ratings:


 1 Reply(s)
 200  View(s)
Rate this:
I want to use ComboBox in DataGridView Windows Application c#. please help me



aditya kumar Patel

Total Post:242

Points:1722
Posted on    December-13-2015 10:26 PM

The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. The DataGridView control makes it easy to define the basic appearance of cells and the display formatting of cell values.

Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, and font. Typically, however, multiple cells will share particular style characteristics. You can define appearance and formatting styles for individual cells, for cells in specific columns and rows, or for all cells in the control by setting the properties of the DataGridViewCellStyle objects accessed through various DataGridView control properties.

using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.Data;
namespace WindowsForums
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void CreateColumn()
        {
            DataGridViewComboBoxColumn dvgcmb = new DataGridViewComboBoxColumn();
            dvgcmb.HeaderText = "Product Name";
            dvgcmb.Name = "productname";
            dvgcmb.Items.Add("Computer");
            dvgcmb.Items.Add("Mobile");
            dvgcmb.Items.Add("Laptop");
            dvgcmb.Items.Add("Mouse");
            dvgcmb.Items.Add("Keyboard");

            dataGridView1.Columns.Add("id", "ID");
            dataGridView1.Columns.Add(dvgcmb);
            dataGridView1.Columns.Add("quantity", "Quantity");
            dataGridView1.Columns.Add("rate", "Rate");
            dataGridView1.Columns.Add("amount", "Amount");
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            CreateColumn();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int index=e.RowIndex;
           decimal quantity=Convert.ToDecimal( dataGridView1.Rows[index].Cells["quantity"].Value);
           decimal rate = Convert.ToDecimal(dataGridView1.Rows[index].Cells["rate"].Value);
           dataGridView1.Rows[index].Cells["amount"].Value = (quantity * rate);
        }
        
        
    }


}

 


Don't want to miss updates? Please click the below button!

Follow MindStick