Results 1 to 9 of 9

Thread: [RESOLVED] ComboBox in Datagridview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    90

    Resolved [RESOLVED] ComboBox in Datagridview

    Im hoping to get the position of a cell in a datagridview so that I can position a combobox over the cell on the cellEnter method.
    How do I get the Left and Top positions of a selected cell?
    (This was easy in VB6 MSHFLEXGRID)

    I see this question was asked here once before but was either never resloved/marked resolved...

    or
    Even better than that...
    I see there is a combobox option in .Net 2005 which I cant seem to get to work.
    How can I apply a dropdown to cells in a given column (or to an idividual cell/s within the column - whichever is easiest) ?

    My datagridview is bound to data.

    Thanks...

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

    Re: ComboBox in Datagridview

    You simply set the type of the column to DataGridViewComboBoxColumn and then set the DataSource, DisplayMember and ValueMember of the colum exactly as you would for a regular ComboBox. Note that the grid itself will be bound to different data than the column. The column is bound to the data from which the user is allowed to choose a value for that column.
    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
    Lively Member
    Join Date
    Oct 2005
    Posts
    90

    Re: ComboBox in Datagridview

    ok so if I understand this the DataGridViewComboBoxColumn will not work for me?

    The column I want the dropdown to appear in is a column that is bound to my data. (eg if my combo choices were stuff,items,things)

    When form opens the exising data is displayed:

    ProductNo Type(drop) Qty
    1 stuff 2
    2 things 5
    3 items 4

    I offer the ability to add new/change/insert/delete product lines on the grid before finally passing all back to the SQL database.

    So in the above example, I right click on the grid, select 'Add New', this adds a new line, with ProductNo 4.
    The rest of the cells are now able to be filled in. - I just want to make the types selectable.

    This would be so easy if I could just find the Top position of the seleced Cell.

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

    Re: ComboBox in Datagridview

    The DataGridViewComboBoxColumn is exactly what you want. Read the help topic for the class for more information.
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    90

    Re: ComboBox in Datagridview

    jmcilhinney, I have noticed your many responses in these forums to various folks, and it is very clear you know what you are talking about - so I do accept when you say the DataGridViewComboBoxColumn is exactly what I want.

    However:
    Having read the class, it gives 6 steps needed then offers an example which to me looks a HUGE amount of code in order to acheive this.

    I was hoping this would be relativly easy to do but am now concerned this seems an overly complicated thing to do as I take my early steps into .NET.

    As an easier alternative,
    Is there not a way to find the top position of a selected cell ?
    I can then do this like in VB6 using only a few lines of code.

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

    Re: ComboBox in Datagridview

    I can't believe that you would consider it easier to float an unrelated ComboBox over a DataGridView by finding the coordinates of a cell. This is all about data binding. if you bind your data properly in the designer it takes no code whatsoever to accomplish what you want to do. Take a look at the project I've attached. I added a DataGridView, two BindingSources and a DataSet with two DataTables to the form in the designer. I set up the appropriate data bindings in the designer and voila: exactly what you're looking for. The only code I had to write was to populate the parent table.
    Attached Files Attached Files
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    90

    Re: ComboBox in Datagridview

    Cheers - that's really good of you to have sent that.
    This does look like what I'm describing, so I will try to recreate your example to gain a better understanding of how it fits together.
    (which is what it's all about I s'pose)

    I currently have not added a datasource or binding sources.
    I created a module which contained my SQL connection data, then filled an adapter with a table or something.

    Hopefully I can apply your excample to my exising project.

    If all else fails though...how do I get the cell position? (as a backup)
    Are you just not telling in order to force me to use what you know to be the correct way?

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

    Re: ComboBox in Datagridview

    I'm not commenting on how to get the cell coordinates because I don't know and I don't envision ever needing to know so I don't intend to investigate to find out.

    Note that when you see a list of explicit instructions it can look long and complex, but that doesn't mean that implementing it is. Here's what I did to create that project:
    1. Add a DataGridView to the form.
    2. Add a DataSet to the form.
    3. Add two DataTables to the DataSet.
    4. Add two columns to the first table.
    5. Add three columns to the second table.
    6. Add two BindingSources to the form.
    7. Set the DataSource of each BindingSource to the DataSet.
    8. Set the DataMember of the first BindingSource to the first table.
    9. Set the DataMember of the second BindingSource to the second table.
    10. Set the DataSource of the DataGridView to the second BindingSource. Note that this creates the columns in the grid automatically.
    11. Change the type of the second grid column to DataGridViewComboBoxColumn.
    12. Set the DataSource of the second grid column to the first BindingSource.
    13. Set the DisplayMember second grid column to the Name column and the ValueMember to the ID column.
    14. Add code to the form's Load event handler to populate the first DataTable to provide a selection list in the second grid column.
    At a glance, that could be construed as complex, but it took me all of three minutes to implement. All you would need to do would be to change the schema of the tables to match your data and add data access components to Fill the tables from the database. Note that the IDE also has tools to get the schema from the database for you and build a strongly typed DataSet with no code. The VS designer can do a lot for you.
    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    90

    Re: ComboBox in Datagridview

    Thanks again - Ill give this a go

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