Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

DataGridView control with ListBox

by the3factory 3/7/2008 8:08:00 AM
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 enter to a cell it is not popup that displaying content of it. Another thing is there are limited numbers of component support with DataGridView. Such as DataGridViewButtonColumn, DataGridViewCheckBoxColumn, DataGridViewComboBoxColumn, etc. It is difficult to add additional component to DaraGridView control.

In this article I have discussed how to develop a control that inheriting standard features of DataGridView and add 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 it’s DLL to your solution.

How_to_add_control_to_your_project.JPG

How to add this control in to 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 add ListBox, Columns should be added to the NewDataGridView. Listbox can be added through of property window of NewDataGridView control. Use ListBox Details section.

Add_listbox_to_DataGridView.JPG

After adding a ListBox you should bind it with 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 have added to the NewDataGridView component for assign selected item to cell and hide that ListBox when double clicked on the listbox. Here is the code for that.

Collapse
            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 selecte item of ListBox into appropiate 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();
}
}
}

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0