Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / C#
Alternative
Article

Embedding a DataGridView in a ComboBox

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Jun 2016CPOL 13.1K   16   6   3
This is an alternative for "Embedding a DataGridView in a ComboBox"
In this tip, you will see an alternative implementation in C# of GridComboBox

Background

This project is based on the article, Embedding a DataGridView in a ComboBox, whose code is in VB.

Using the Code

Output of this project is a .dll file. You must attach this file in your project and use it like a control of Windows Forms.

Keep in Mind

  • You have to add a simple DataGridView in your project, this element will be controlled by GridComboBox, specially visibility states.

This is a small brief of how to use this component in your project:

C#
using System;
using TemporalAPP.GraphicSourceDataSetTableAdapters;
using System.Data;
using System.Windows.Forms;
using static TemporalAPP.GraphicSourceDataSet;
using GridComboBox;

namespace TemporalAPP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ArticuloArancelDataTable tabla = new ArticuloArancelDataTable();
            new ArticuloArancelTableAdapter().Fill(tabla);
            dataGridView1.DataSource = tabla; // whatever datasource of DataGridView
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.Columns[0].Width = 1;
            accGridComboBox1.AddDataGridView(dataGridView1, false); // SO IMPORTANT!
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            dataGridView1.Visible = true;
        }

        private void dataGridView1_CellMouseDoubleClick
                     (object sender, DataGridViewCellMouseEventArgs e)
        {
            if (accGridComboBox1.SelectedValue == null) return;
            accGridComboBox1.Text = 
               ((DataRowView)accGridComboBox1.SelectedValue).Row["Codigo"].ToString();
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Return || e.KeyCode == Keys.Enter)
            {
                if (accGridComboBox1.SelectedValue == null) return;
                accGridComboBox1.Text = 
                   ((DataRowView)accGridComboBox1.SelectedValue).Row["Codigo"].ToString();
            }
        }
    }

Important Notes

Default visibility state of DataGridView is true, so you have to do double click on GridComboBox control until it shrinks; after that, you can use it. I was considering it to be an issue.

First Run

Image 1

After Initial Double Click

Image 2

After Select an Item

Image 3

Point of Interest

I would like if someone can help with the initial state of the DataGridView. In my opinion, it would be better with visibility=false; to avoid initial double click.

History

  • 3rd June, 2016: Initial version

License

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


Written By
Software Developer Graphicsource
Ecuador Ecuador
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood article Pin
Member 110824806-Jun-16 22:16
Member 110824806-Jun-16 22:16 
AnswerRe: Good article Pin
César Jaramillo L.5-Jul-16 4:11
professionalCésar Jaramillo L.5-Jul-16 4:11 
QuestionNice Pin
LeaPatine3-Jun-16 3:04
LeaPatine3-Jun-16 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.