Results 1 to 2 of 2

Thread: Question about comboboxcolumn on a DataGridview

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Question about comboboxcolumn on a DataGridview

    If I create a comboboxcolumn on a DataGridview does it have a SelectedIndexChanged event?

    I want some code to run depending on a users selection. I know I can do this with standard Comboboxes on a form I'm just wondering if this can be done on a Datagridview as well

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

    Re: Question about comboboxcolumn on a DataGridview

    The column can't have a SelectedIndexChanged event because it contains numerous cells, each of which have their own SelectedIndex. In actual fact, it's not even the cells that have a SelectedIndex. It's actually the ComboBox controls embedded in the cells, and that only actually happens when a cell is being edited the rest of the time the cell is actually empty and the combo box appearance is simply drawn on.

    What you need to do is handle the EditingControlShowing event of the grid, which is raised whenever a cell starts an editing session and an editing control is shown. You can get a reference to that control, which, for your combo box column, will be a ComboBox, and then attach an event handler to its SelectedIndexChanged event. Just note that you MUST detach the event handler again when you're done.

    Assuming that you're using VB.NET, you should search the VB.NET forum for examples of handling the EditingControlShowing event. You should also have posted this question in that forum in the first place as it has nothing to do with databases. The data in a DataGridView can come from anywhere and this no relationship to accessing a database.
    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

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