none
Need DataGridView Read/Write Example ComboBox

    Question

  • VB2005, SQL Server 2000

    The MS supplied 101 sample DataGridView ComboBoxColumn example is read only which is kinda dumb since I'm not sure when one would ever need a read only dropdown.

    Has anyone seen a KISS example of connecting to a SQL Server 2000 table (spmething like the PUBS DB, authors table) using a DataGridView with a combo, say on the State field? Need to see something that does not use the DataSource wizard objects.

    In general: Is it possible to bind to a dataset using the auto generated columns and then change a column type to Combo? Or does one have to construct all columns manually if any are other than the auto generated type?

    TIA
    Wednesday, August 10, 2005 4:51 PM

Answers

  • As far as late binding the DataGridView, it is really easy -- just set the grid's DataSource property. By default the AutoGenerateColumns property is true so it will create the columns. You can set AutoGenerateColumns to false before setting the grid's DataSource property and then add columns yourself (dataGridView.Columns.Add(...)). Set the DataPropertyName value to bind the column. Regarding the combobox, you can dynamically add a combobox column just like a normal column. Set the combobox's DataPropertyName, and create a datasource for the combobox column.

    There are a couple of samples in the documentation that show you how to manually databind the grid. Here is one that also shows how to manually databind a combobox column: http://msdn2.microsoft.com/en-us/library/y0wfd4yz.aspx
    (EDIT: updated link)

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

    Monday, December 12, 2005 7:07 PM

All replies

  • I don't understand the "101 sample DataGridView ComboBoxColumn example" comment, the DataGridViewComboBoxColumn supports read/write, it just doesn't support adding new values to the drop-down (it only supports the ComboBox.DropDownStyle = DropDownList)

    I'm not familure with ADO objects not using the DataSource wizard, so you'll need to post a question to the .NET Framework Data Access and Storage forum: http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=45 

    Here are the steps that show how to change the column type at design time to a combobox. This example uses the DataSource Wizard:

    1) New Windows Forms app
    2) Click The Add new datasource option and connect to the Northwind database
    3) Select the Products and Categories tables and finish the wizard
    4) Drag and drop the Products table from the data sources window to your form to create and databind a DataGridView
    5) Right click on the DataGridView and choose Edit Columns
    6) Select the CategoryID column and for the column type property change it to a DataGridViewComboboxColumn
    7) Goto the DataSource property for the column and expand that until you get to the Categories table
    8) Set the DisplayMember to be CategoryName and the ValueMember to be the CategoryID
    9) Click ok to dismiss the Edit
    10) Run

    Hope this helps,
    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

    Thursday, August 11, 2005 8:04 PM
  • This seems like this is the best forum for VS2005B2 DataGridView(DGV) control questions. Problem appears to be that few posters have knowledge of how to use the DGV in a late bound fashion. Samples and posts push the designer solutions which are not useful in late-bound situations. My questions are control specific and the underlying DB and its access methods are moot.

    For example I have a task to create simple maintenance forms for 30-40 lookup tables. There is little/no business logic attached to each table but in many cases foreign key data is stored that would be best to populate using a combobox. I'd like to create a form with DGV control that is late bound to one of the 30-40 tables as needed.

    Is the easiest way to do this to late-bind the DGV, then add an unbound column that is of type combobox, and then use the CellEndEdit event to move the selected data to the cooresponding bound column? (not sure the terminology is correct) Would that even work - mixing bound and unbound columns?

    Best would be to convert a late-bound text column to the combobox type but that doesn't seem like it can be done.

    I am fine with adding the combo box Items without binding to a table, but a late-bound items list would be good too.

    See http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=71917 for other details/trials/tribulations.

    Ideas?
    Tuesday, August 16, 2005 6:08 PM
  • As far as late binding the DataGridView, it is really easy -- just set the grid's DataSource property. By default the AutoGenerateColumns property is true so it will create the columns. You can set AutoGenerateColumns to false before setting the grid's DataSource property and then add columns yourself (dataGridView.Columns.Add(...)). Set the DataPropertyName value to bind the column. Regarding the combobox, you can dynamically add a combobox column just like a normal column. Set the combobox's DataPropertyName, and create a datasource for the combobox column.

    There are a couple of samples in the documentation that show you how to manually databind the grid. Here is one that also shows how to manually databind a combobox column: http://msdn2.microsoft.com/en-us/library/y0wfd4yz.aspx
    (EDIT: updated link)

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

    Monday, December 12, 2005 7:07 PM
  • Thanks Mark!

    The step 1 to 10 above was really helpful, works fine.

    Are there something similar when using the Detail-form, instead of the default DataGridView?

    Can't find the same type of properties.

    Sten-Gunnar

    Friday, December 01, 2006 1:16 AM
  •  Bob Heitzman wrote:
    VB2005, SQL Server 2000

    The MS supplied 101 sample DataGridView ComboBoxColumn example is read only which is kinda dumb since I'm not sure when one would ever need a read only dropdown.

    Has anyone seen a KISS example of connecting to a SQL Server 2000 table (spmething like the PUBS DB, authors table) using a DataGridView with a combo, say on the State field? Need to see something that does not use the DataSource wizard objects.

    In general: Is it possible to bind to a dataset using the auto generated columns and then change a column type to Combo? Or does one have to construct all columns manually if any are other than the auto generated type?

    TIA
    Tuesday, January 23, 2007 1:59 PM