Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to insert combo box in datagridview cell in VB.net 2005?
 

How to insert combo box in datagridview cell in VB.net 2005?

I am developing a windows application in C#.Net 2005.
On one form there is a grid in which there are only two columns - Employee_Designation, Employee_Name.
Now when user of my application will click on Employee_Designation Cell then one
list of all designation should be populated.
Same should work for Employee_Name.
Also these two list will be coming from two database tables Designation_Master, Employee_Master.
I already have two datatables with these Designation_Master, Employee_Master.
Also user can add any number of rows in this datagrid.
And I want to do it at runtime.

Also I want the textchanged event of those combo boxes.


Please note that i want to do this at runtime ( not from design ) and also in Windows application.

Harsh1  Wednesday, April 30, 2008 2:22 PM
Did you tried DataGridViewComboBoxColumn or create one derived from it?
You can hook up your events in the EditingControlShowing event.

Sheng Jiang 蒋晟  Wednesday, April 30, 2008 4:12 PM

Hi Harsh,

Based on my understanding, what Sheng Jiang has advised is practical. DataGridViewComboBoxColumn can work perfectly as you like.

I would like to suggest you write code like following:

1. Initialize two BindingSources, and bind them separately to Designation_Master and Employee_Master

Code Snippet

connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\temp\\MyDatabaseData2.MDF;Integrated " +

"Security=True;Connect Timeout=30;User Instance=True");

// Initialization on table1.

command1 = new SqlCommand("select Employee_Designation from Designation_Master;", connection);

dataAdapter1 = new SqlDataAdapter();

bindingSource1 = new BindingSource();

dataAdapter1.SelectCommand = command1;

dataAdapter1.Fill(Designation_Table);

bindingSource1.DataSource = Desination_Table;

// Initialization on table2.

command2 = new SqlCommand("select Employee_Name from Employee_Master;", connection);

dataAdapter2 = new SqlDataAdapter();

bindingSource2 = new BindingSource();

dataAdapter2.SelectCommand = command2;

dataAdapter2.Fill(Employee_Table);

bindingSource2.DataSource = Employee_Table;

2. Bind two ComboBoxColumn separately to these two BindingSources.

Code Snippet

this.Column1.DataSource = bindingSource1;

this.Column1.DisplayMember = "Employee_Designation";

this.Column2.DataSource = bindingSource2;

this.Column2.DisplayMember = "Employee_Name";

3. Implement DataGridView.EditingControlShowing event, in order to make lists of values up to date.

Code Snippet

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

bindingSource1.EndEdit();

bindingSource2.EndEdit();

bindingSource1.ResetBindings(false);

bindingSource2.ResetBindings(false);

}

Hope it helps. If there is anything unclear, please feel free to let me know.

Best wishes,

Jun Wang

Jun Wang Tim  Tuesday, May 06, 2008 8:24 AM
Did you tried DataGridViewComboBoxColumn or create one derived from it?
You can hook up your events in the EditingControlShowing event.

Sheng Jiang 蒋晟  Wednesday, April 30, 2008 4:12 PM

Could you please give me some sample code

Harsh1  Friday, May 02, 2008 1:24 PM

Hi Harsh,

Based on my understanding, what Sheng Jiang has advised is practical. DataGridViewComboBoxColumn can work perfectly as you like.

I would like to suggest you write code like following:

1. Initialize two BindingSources, and bind them separately to Designation_Master and Employee_Master

Code Snippet

connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\temp\\MyDatabaseData2.MDF;Integrated " +

"Security=True;Connect Timeout=30;User Instance=True");

// Initialization on table1.

command1 = new SqlCommand("select Employee_Designation from Designation_Master;", connection);

dataAdapter1 = new SqlDataAdapter();

bindingSource1 = new BindingSource();

dataAdapter1.SelectCommand = command1;

dataAdapter1.Fill(Designation_Table);

bindingSource1.DataSource = Desination_Table;

// Initialization on table2.

command2 = new SqlCommand("select Employee_Name from Employee_Master;", connection);

dataAdapter2 = new SqlDataAdapter();

bindingSource2 = new BindingSource();

dataAdapter2.SelectCommand = command2;

dataAdapter2.Fill(Employee_Table);

bindingSource2.DataSource = Employee_Table;

2. Bind two ComboBoxColumn separately to these two BindingSources.

Code Snippet

this.Column1.DataSource = bindingSource1;

this.Column1.DisplayMember = "Employee_Designation";

this.Column2.DataSource = bindingSource2;

this.Column2.DisplayMember = "Employee_Name";

3. Implement DataGridView.EditingControlShowing event, in order to make lists of values up to date.

Code Snippet

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

bindingSource1.EndEdit();

bindingSource2.EndEdit();

bindingSource1.ResetBindings(false);

bindingSource2.ResetBindings(false);

}

Hope it helps. If there is anything unclear, please feel free to let me know.

Best wishes,

Jun Wang

Jun Wang Tim  Tuesday, May 06, 2008 8:24 AM

Please tell me how to gettext change event of this combo box

Harsh1  Tuesday, May 06, 2008 1:30 PM

Hi,

In my opinion, if you want to do things related to combo box's text while user is inputing value, it would be better to use Validating Event.

Control.Validating Event happens when user is changing its text.

For more information about Validating Event,please refer to:

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox_events.aspx

If you want to do things with Text Changed in your specific scenario,where Validating cannot help, I would appreciate that you provide more information on how to use Text Changed.

Thanks.

Best wishes,

Jun Wang

Jun Wang Tim  Tuesday, May 06, 2008 2:39 PM

You can use google to search for other answers

Custom Search

More Threads

• Stop them from moving a form
• Problem with VS 2003 user controls in VS 2005 RC1 Toolbox
• Disappearing Controls Visual Studio 2005
• Designer error
• textbox visible property
• Forms Designer and GetDC() vs. GetWindowDC()
• ITypeDescriptorContext type mismatches after building
• Argh: Label.Padding.All
• Difference between DrawGrid, ShowGrid
• Custom events not showing up under Properties/Events in Designer?