Results 1 to 3 of 3

Thread: Convert TextBox to a ComboBox in a DataGridView

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    7

    Question Convert TextBox to a ComboBox in a DataGridView

    Hi All,

    I have a DataGridView control. When editing a row in it, I need the user to be able to select a value from a combo box. When the user is not editing a row, however, I need the column to display data that is potentially NOT in the list of items in the combo box.

    There were two different solutions that I came up with, although I know how to do neither. I would also be open to any other solution that you guys can offer.

    The first solution that I came up with (but again, have no idea how to accomplish) was to have the column be based on the TextBox column and when the user was editing the cell, somehow change it to a dropdown, and then process the value that the user selected when they moved off of the cell.

    The second solution that I thought was possible was to somehow make VB allow and display values in the column that were not in the combobox items. This is the solution that seems the most likely to succeed, but again I have no idea how to do it. As soon as I bind data to the ComboBox column, it seems to not want to display items that are not in the combobox, and throws an error when I attempt to do so.

    I would appreciate any help in this issue!


    Thanks in advance!

    -Funger

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert TextBox to a ComboBox in a DataGridView

    Here's how it works. Just like all DataGridViewCells, the DataGridViewComboBox does not contain a control by default. Doing so would make things slow to a crawl. The cell contains a Value which is rendered in the cell, and that's what the user sees. Now, when you start editing a cell a ComboBox control is created, or an existing one reused, and it is embedded in the cell. The Value property of the cell itself is assigned to the SelectedValue property of this ComboBox control. Just like any normal ComboBox, if there is no matching value an exception will be thrown.

    The default behaviour for the DataGridViewComboBoxColumn is to display ComboBox controls with their DropDownStyle property set to DropDownList. What you would need to do would be to change that such that the DropDownStyle was set to DropDown and the editing control allow values other than what's in the list. There is no way to do this through the standard DataGridViewComboBoxColumn or DataGridViewComboBoxCell classes. You could access the editing control itself from the outside, i.e. in your form's code, but that would be messy. The "proper" way to do this would be to create your own custom types. You would have to inherit the DataGridViewComboBoxCell class, and probably the column too, and add your own custom functionality to support this behaviour. You may have to inherit the DataGridViewComboBoxEditingControl class too, but I wouldn't think so. That is the class that inherits ComboBox and implements IDataGridViewEditingControl, thus providing a combo box for editing a DataGridView cell.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    7

    Re: Convert TextBox to a ComboBox in a DataGridView

    jmcilhinney,

    Thanks for your reply. I assumed that I was going to have to extend the functionality of either the DataGridViewTextBoxColumn class or the DataGridViewComboBoxColumn class. The problem is I'm not really sure where to start. Do you by chance have an example that I could follow, or a link to one that would point me in the right direction? Thanks!

    -Funger

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width