By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

DataGridView and ComboBox

P: n/a
I am sure this has been asked and answered, but here goes anyway...

VS.Net 2005, VB.Net

How can you display more than one field in the displaymember property of a
combobox inside the datagridview control? I am at a loss.

Thanks,
David
Aug 30 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
David,

You cannot in any combobox of Net use more columns in the displaymember of a
combobox. There is a standard multicolumn combobox to find on InterNet but I
have never seen such a one for inside another control.

If you want to display however by instance a first name and a last name
using a bounded table, than you can create an extra column in you datatable.
If you don't know how, reply.

I hope this helps,

Cor

<da******@newsgroups.nospamschreef in bericht
news:Om**************@TK2MSFTNGP05.phx.gbl...
>I am sure this has been asked and answered, but here goes anyway...

VS.Net 2005, VB.Net

How can you display more than one field in the displaymember property of a
combobox inside the datagridview control? I am at a loss.

Thanks,
David

Aug 31 '06 #2

P: n/a
Hi David,

Based on my understanding, you want to display a dropdown list of items in
each Combobox of DataGridViewComboBoxColumn. If I have misunderstood you,
please feel free to tell me, thanks.

The programming model of DataGridViewComboBoxColumn can be found in the
link below, also, there is sample code in the link of populating the
combobox with a list of items:
http://msdn2.microsoft.com/en-us/lib...datagridviewco
mboboxcolumn.aspx

We can populate the dropdownlist of combobox in DataGridViewComboBoxColumn
with DataGridViewComboBoxColumn.Items property. Please refer to
SetAlternateChoicesUsingItems method in the above sample code.

The key point is that the items populated in the list must be correspond to
the datasource data value of DataPropertyName from DataGridView
databinding. The datasource data of DataPropertyName is used as the
SelectedValue property of the Combobox. The DataGridView databinding
requires looking up the value in the combobox items and getting the display
value for the item. During this process, if the cell's value cannot be
found in the combo box items collection it raises the DataError event.
Let's take the above sample code as an example:
In above DataGridView databinding, the DataGridView will display employees
table from northwind sample database.
DataGridViewComboBoxColumn.DataPropertyName is set to *TitleOfCourtesy*
field in CreateComboBoxColumn() method. So, to populate the
DataGridViewComboBoxColumn.Items property, we should take care to assign
items corresponding to the data of *TitleOfCourtesy* field, such as "Mr.",
"Ms.", "Mrs.", "Dr.". If we assign other data to the Items property instead
of above 4 candidate values, the databinding will fail to find a
selectedvalue in dropdownlist, and DataError event is raised.

Once you take care of this key point, you should can use
DataGridViewComboBoxColumn without any problem.

Additionally, Microsoft .Net Winform team has maintained a wonderful
DataGridView FAQ with samples in the link below. After you download it, in
the DataGridView FAQ, you will find a lot of guidelines regarding using
DataGridView, also a dedicated topic with DataGridViewComboBoxColumn in
section 3.5:
http://www.windowsforms.net/Samples/...mId=220&tabind
ex=4

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 31 '06 #3

P: n/a
Oh, after viewing Cor's reply, I suspect I have misunderstood you. It seems
that you are looking for a multicolumn combobox in DataGridView.

Yes, just as Cor pointed out, there is no build-in combobox support for
this, since DataGridView internally leverages combobox, so we can not get
this in DataGridView.

However, we can customize to create a multicolumn combobox in .Net, and
since the DataGridView has a rich feature of hosting controls, we can
create a customized DataGridViewMultiColumnComboBoxColumn to host this
multicolumn combobox.

To create a multicolumn combobox, we have several solutions, some use
ListView to host in the dropdown list, some use other options, please refer
to the 2 articles below as a start:
http://www.codeproject.com/vb/net/multicolumncombo.asp
http://www.codeproject.com/vb/net/Mu...nFlatCombo.asp

Hope this helps and sorry for the misunderstanding.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 31 '06 #4

P: n/a
Jeffrey,

Thank you for the showing us that sample page. A pity is that the vb samples
that I saw are obvious by a converter created C# code and in my idea not in
VB.Net style as the VB team would make them.

As well are those I have seen, when they are binded, based on the Northwind
dataset. Special because for this reason we did make this weekend a sample
about a combobox binded to a dataset on the fly. I saw no combobox sample in
what you have showed us, so therefore does this maybe add to this thread.

http://www.vb-tips.com/default.aspx?...5-29ce9ff01d75

:-)

I hope this helps,

Cor

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comschreef in bericht
news:X5**************@TK2MSFTNGXA01.phx.gbl...
Hi David,

Based on my understanding, you want to display a dropdown list of items in
each Combobox of DataGridViewComboBoxColumn. If I have misunderstood you,
please feel free to tell me, thanks.

The programming model of DataGridViewComboBoxColumn can be found in the
link below, also, there is sample code in the link of populating the
combobox with a list of items:
http://msdn2.microsoft.com/en-us/lib...datagridviewco
mboboxcolumn.aspx

We can populate the dropdownlist of combobox in DataGridViewComboBoxColumn
with DataGridViewComboBoxColumn.Items property. Please refer to
SetAlternateChoicesUsingItems method in the above sample code.

The key point is that the items populated in the list must be correspond
to
the datasource data value of DataPropertyName from DataGridView
databinding. The datasource data of DataPropertyName is used as the
SelectedValue property of the Combobox. The DataGridView databinding
requires looking up the value in the combobox items and getting the
display
value for the item. During this process, if the cell's value cannot be
found in the combo box items collection it raises the DataError event.
Let's take the above sample code as an example:
In above DataGridView databinding, the DataGridView will display employees
table from northwind sample database.
DataGridViewComboBoxColumn.DataPropertyName is set to *TitleOfCourtesy*
field in CreateComboBoxColumn() method. So, to populate the
DataGridViewComboBoxColumn.Items property, we should take care to assign
items corresponding to the data of *TitleOfCourtesy* field, such as "Mr.",
"Ms.", "Mrs.", "Dr.". If we assign other data to the Items property
instead
of above 4 candidate values, the databinding will fail to find a
selectedvalue in dropdownlist, and DataError event is raised.

Once you take care of this key point, you should can use
DataGridViewComboBoxColumn without any problem.

Additionally, Microsoft .Net Winform team has maintained a wonderful
DataGridView FAQ with samples in the link below. After you download it, in
the DataGridView FAQ, you will find a lot of guidelines regarding using
DataGridView, also a dedicated topic with DataGridViewComboBoxColumn in
section 3.5:
http://www.windowsforms.net/Samples/...mId=220&tabind
ex=4

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Aug 31 '06 #5

P: n/a
Jeffrey,

It are a lot of replies from me today, I find this multicolumn combobox much
nicer. The ones you show are easier to do with just an extra datatable
column with an expression in that as I tried to tell in my first reply.

However this one is very nice and easy to use.

http://www.planet-source-code.com/vb...2934&lngWId=10

:-)

I send it here because it add to the thread in my idea.

Cor

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comschreef in bericht
news:I2*************@TK2MSFTNGXA01.phx.gbl...
Oh, after viewing Cor's reply, I suspect I have misunderstood you. It
seems
that you are looking for a multicolumn combobox in DataGridView.

Yes, just as Cor pointed out, there is no build-in combobox support for
this, since DataGridView internally leverages combobox, so we can not get
this in DataGridView.

However, we can customize to create a multicolumn combobox in .Net, and
since the DataGridView has a rich feature of hosting controls, we can
create a customized DataGridViewMultiColumnComboBoxColumn to host this
multicolumn combobox.

To create a multicolumn combobox, we have several solutions, some use
ListView to host in the dropdown list, some use other options, please
refer
to the 2 articles below as a start:
http://www.codeproject.com/vb/net/multicolumncombo.asp
http://www.codeproject.com/vb/net/Mu...nFlatCombo.asp

Hope this helps and sorry for the misunderstanding.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Aug 31 '06 #6

P: n/a
Everyone, thanks for all the information. I will let you know which option
I choose and post some code for everyone to look at.

David

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:I2*************@TK2MSFTNGXA01.phx.gbl...
Oh, after viewing Cor's reply, I suspect I have misunderstood you. It
seems
that you are looking for a multicolumn combobox in DataGridView.

Yes, just as Cor pointed out, there is no build-in combobox support for
this, since DataGridView internally leverages combobox, so we can not get
this in DataGridView.

However, we can customize to create a multicolumn combobox in .Net, and
since the DataGridView has a rich feature of hosting controls, we can
create a customized DataGridViewMultiColumnComboBoxColumn to host this
multicolumn combobox.

To create a multicolumn combobox, we have several solutions, some use
ListView to host in the dropdown list, some use other options, please
refer
to the 2 articles below as a start:
http://www.codeproject.com/vb/net/multicolumncombo.asp
http://www.codeproject.com/vb/net/Mu...nFlatCombo.asp

Hope this helps and sorry for the misunderstanding.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Aug 31 '06 #7

P: n/a
Hi David,

Thanks for your feedback!

Ok, I will continue to monitor this thread. Please feel free to feedback
any progress and concerns, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 1 '06 #8

P: n/a
Hi David,

How about this issue? Have you managed to customize these samples to your
need? Is your problem resolved?

If you still have any concern or need any help, please feel free to
feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 5 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.