Click here to Skip to main content
15,881,580 members
Articles / Programming Languages / C#

DataGridView Control with ListBox

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
6 Mar 2008CDDL1 min read 146.4K   6.7K   44   5
In this article, I have discussed how to develop a control that inherits standard features of DataGridView and adds additional features to that control. By locking this NewDataGridView control, you will be able to develop a DataGridView control adding many more features as you wish.
Introduction.JPG

Introduction

We can add DataGridViewComboBoxColumn control to a specified column of a Standard DataGridView control that comes with .NET. There are some limitations with that control such as when entering a cell it is not popup that displays its content. Another thing is there is limited number of component support with DataGridView such as DataGridViewButtonColumn, DataGridViewCheckBoxColumn, DataGridViewComboBoxColumn, etc. It is difficult to add additional components to DaraGridView control.

In this article, I have discussed how to develop a control that inherits standard features of DataGridView and adds additional features to that control. By locking this NewDataGridView control, you will be able to develop a DataGridView control adding many more features as you wish.

Using the Code

How to add this control to your project?

Add AdvDataGridView and AdvListBox projects or its DLL to your solution.

How_to_add_control_to_your_project.JPG

How to Add this Control into your Form?

Drag and drop NewDataGridView control from ToolBox into your form.

How_to_add_control_to_your_form.JPG

How to add ListBox to DataGridView Control?

After adding NewGridView control to your form, you should add ListBox according to your requirements. Note: Before you add ListBox, columns should be added to the NewDataGridView. Listbox can be added through the property window of NewDataGridView control. Use ListBox Details section.

Add_listbox_to_DataGridView.JPG

After adding a ListBox, you should bind it with a particular column of NewDataGridView. To do that, you have to use Binding Column section of property window of ListBox.

Set_ListBox_to_Column.JPG

Now you have to define EventHandler for each ListBox that you have added to the NewDataGridView component for assigning a selected item to a cell and hide that ListBox when double clicked on the listbox. Here is the code for that:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Test_Application
{
    public partial class Form1 : Form
    {
        int rowIndex, columnIndex;

        public Form1()
        {
            InitializeComponent();

            //EventHandler for ListBox that added to the DataGridView.
            this.newDataGridView1.ListBoxCollection[0].DoubleClick += 
                new EventHandler(ListBox1_DoubleClick);
        }

        /// <summary>
        /// When ListBox is double clicked, called this method.
        /// Add select item of ListBox into appropriate cell of the DataGridView.
        /// After then hide ListBox.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ListBox1_DoubleClick(object sender, EventArgs e)
        {
            this.newDataGridView1[columnIndex, rowIndex].Value = 
                this.newDataGridView1.ListBoxCollection[0].SelectedItem.ToString();

            this.newDataGridView1.ListBoxCollection[0].Visible = false;
        }

        /// <summary>
        /// Get row index and column index of selected cell of the DataGridView.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newDataGridView1_CellEnter
		(object sender, DataGridViewCellEventArgs e)
        {
            rowIndex = e.RowIndex;
            columnIndex = e.ColumnIndex;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Add new row to the DataGridView.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddRow_Click(object sender, EventArgs e)
        {
            this.newDataGridView1.Rows.Add();
        }
    }
}

History

  • 7th March, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer (Senior)
Sri Lanka Sri Lanka
I'm working with SAP and Microsoft Technologies such as C#, MS SQL server, ASP.NET, ASP.NET MVC, WebAPI.

Comments and Discussions

 
QuestionAdding comboboxCheckBoxTo the DataGrid Pin
Member 1583582321-Nov-22 19:57
Member 1583582321-Nov-22 19:57 
Questionllist box Pin
Dheeraj Kumar Pentela8-Mar-12 2:57
Dheeraj Kumar Pentela8-Mar-12 2:57 
GeneralError After implementing NewDataGridView Pin
RavindraJumrani30-Jul-08 5:03
RavindraJumrani30-Jul-08 5:03 
Dear Manjula,

You article was very helpful to me. But when i implemented this on my project and then closing the designer form and again re-opening it gives me the following error

Object reference not set to an instance of an object.
Hide

at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)

Any help will be grateful.

Thanks & Regards,

Ravi
GeneralRe: Error After implementing NewDataGridView Pin
RavindraJumrani24-Sep-08 0:52
RavindraJumrani24-Sep-08 0:52 
GeneralRe: Error After implementing NewDataGridView Pin
engnog26-Dec-09 4:18
engnog26-Dec-09 4:18 

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.