CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2010
    Posts
    8

    Hotkey(s) with DataGridView class

    Hey everyone, so here is my situation.
    .Net 3.5
    I have a DGV with many cells in it. One type of cell is a cell that has a drop down when editing.

    I would like to allow the user to Alt + Down to edit these types of cells.

    Here is the code I currently have within the KeyDown event handler.
    Code:
    if (e.KeyCode == Keys.Down && Control.ModifierKeys == Keys.Alt)
         {
              DataGridViewCell cell = this.gvMain.CurrentCell;
              DataGridViewColumn col = cell.OwningColumn;
              MetadataAttribute attribute = (MetadataAttribute)col.Tag;
    
              if (cell != null && attribute != null && attribute.AttributeType == AttributeType.Domain && attribute.DataType == AttributeDataType.Text)
                    {
                            DataGridViewCellCancelEventArgs ce = new DataGridViewCellCancelEventArgs(this.gvMain.CurrentCell.ColumnIndex, this.gvMain.CurrentCell.RowIndex);
                            this.gvMain_CellBeginEdit(sender, ce);
                            e.Handled = true;
                    }
         }
    What is happening is that it does begin edit mode... but it does not behave as though I clicked into edit mode. When I click into edit mode and hit the down arrow, edit mode will end. But when I Alt + Down into edit mode and then hit the down arrow, instead the cell below is selected and edit mode does not end.

    Is there a way to alleviate this issue? I would prefer not to have to add code to the SelectedCellChanged event handler but I will if necesary.

    Tlr - I would like to properly begin an edit session within a cell from a hot key.
    Last edited by jmachol; July 15th, 2010 at 10:16 AM.

  2. #2
    Join Date
    Jun 2010
    Posts
    8

    Re: Hotkey(s) with DataGridView class

    I've just learned that I can use a DataGrideViewCell type of ComboBox...

    I would like to use the ComboBox column but it seems that if I did I would have to load the information at the point when I add the column to the DGV... which would be a huge performance hit.

    So what I would now need to do is the latter of the following quote.
    When you bind data to a DataGridViewComboBoxColumn you are binding data for every cell in that column. If you don't want the same data in every cell then you can't bind the column. You need to either set the DataSource of each cell separately, or else populate each ComboBox control as it's displayed. For the latter you would have to handle the EditingControlShowing event of the grid.
    I am unsure of how to go about doing this... any advice?

  3. #3
    Join Date
    Oct 2009
    Location
    Harrisburg, PA
    Posts
    23

    Re: Hotkey(s) with DataGridView class

    If you're trying to load a custom datasource into each row then here is what I have done:

    Respond to the EditingControlShowing event of the dgView and pull the combobox editing control from the event args. Next set the datasource of the editing control combox to what ever data you want to.

    If I remember correctly you will need to set the datasource of the dgView column to include all possible values that a user can select in any row (but they will be limited to the subset that you provide for each row)

    In this way your only loading the data as it's needed when a user actually enters the edit mode of the combo box cell.

  4. #4
    Join Date
    Oct 2009
    Location
    Harrisburg, PA
    Posts
    23

    Re: Hotkey(s) with DataGridView class

    when you edit a cell you are actually working with an editing control that is painted on top of the cell. Here this will get you started hopefully:

    Code:
            private const int CBX_COLUMN_OF_INTEREST = 0;
            private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (dataGridView1.CurrentCell.ColumnIndex == CBX_COLUMN_OF_INTEREST)
                {
                    DataGridViewComboBoxEditingControl ctr = e.Control as DataGridViewComboBoxEditingControl;
                    ctr.Items.Add(someitem); //add your items
                    ctr.DataSource = //this may work too
                }
            }

  5. #5
    Join Date
    Jun 2010
    Posts
    8

    Re: Hotkey(s) with DataGridView class

    Quote Originally Posted by JoeBuntu View Post
    when you edit a cell you are actually working with an editing control that is painted on top of the cell. Here this will get you started hopefully:

    Code:
            private const int CBX_COLUMN_OF_INTEREST = 0;
            private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (dataGridView1.CurrentCell.ColumnIndex == CBX_COLUMN_OF_INTEREST)
                {
                    DataGridViewComboBoxEditingControl ctr = e.Control as DataGridViewComboBoxEditingControl;
                    ctr.Items.Add(someitem); //add your items
                    ctr.DataSource = //this may work too
                }
            }
    I'm not sure if Constants would work as the columns of interest change based on which datasource the user decides to load. And they create their own data sources.

  6. #6
    Join Date
    Jun 2010
    Posts
    8

    Re: Hotkey(s) with DataGridView class

    Alright well my team has decided to postpone this hot key due to the fact that it is actually an error with the cell formatting itself.

    Now I am looking to be able to capture ALT modifier + Enter key. The problem is that the system handles enter before it gets to the KeyDown handler. CTRL + Enter works however, which I assume is because CTRL is the master override system key from what I remember.

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

Featured