How to trap key press in DataGridComboBoxColumn

Posted by: anildabral3k on 8 December 2019, 5:15 pm EST

    • Post Options:
    • Link

    Posted 8 December 2019, 5:15 pm EST

    Hi,

    I am looking for a way so that if user types in something in the DataGridComboBoxColumn column and the match is not found, I should be able to show a message. This should happen as soon as the mismatch occurs while user is typing in the editable dropdown in DataGridComboBoxColumn column in C1DataGrid.

    I am using WPF controls ver 4.0.20153.497

  • Posted 8 December 2019, 6:26 pm EST

    Hello,

    Please use the following lines of code to trap the key press in DataGridComboBoxColumn

    <c1:DataGridComboBoxColumn Binding="{Binding IsError}" Header="Column2" >
                       <c1:DataGridComboBoxColumn.CellStyle>
                           <Style TargetType="{x:Type c1:DataGridCellPresenter}">
                               <EventSetter Event="PreviewKeyDown" Handler="Cell_PreviewKeyDown"/>
                            
                           </Style>
                       </c1:DataGridComboBoxColumn.CellStyle>
                           
                       </c1:DataGridComboBoxColumn>
    

    Hope it helps.

    Thanks,

    Mohit

  • Posted 8 December 2019, 10:28 pm EST

    Hi Mohitg,

    Thank you for your response. Instead of XAML could you please send me the equivalent C# code?

    I am using AutoGeneratingColumn event where I already have some other code to do little bit of column customization. I want to put this key-press event code inside with other column customization logic.

    Regards

  • Posted 9 December 2019, 4:26 pm EST

    Hello,

    Please use the following line of code:

     var style = new Style(typeof(C1.WPF.DataGrid.DataGridCellPresenter));
                style.Setters.Add(new EventSetter(C1.WPF.DataGrid.DataGridCellPresenter.PreviewKeyDownEvent, new KeyEventHandler(MainWindow_PreviewKeyDown)));
                (grid.Columns[1] as C1.WPF.DataGrid.DataGridComboBoxColumn).CellStyle = style;
    

    Hope it helps.

    Thanks,

    Mohit

  • Posted 9 December 2019, 6:50 pm EST

    Hi Mohitg,

    Thank you so much. Your C# code worked. I can trap the event.

    Still trying to figure out though, how can I detect the moment when the characters entered in the editable combobox, by an user, does not match with any item in the combobox. I need to show a message at that point. If you could provide some help/hint regarding this that would be great.

    Regards

  • Posted 10 December 2019, 7:54 pm EST

    Hi,

    To detect the moment in which end-user enters a character, you should use C1DataGrid’s BeganEdit event and inside handle C1Combobox’s (editor for DataGridComboBoxColumn) PreviewKeyDown event, as follows:

    void grid_BeganEdit(object sender, DataGridBeganEditEventArgs e)
    {
        if(e.Column.GetType() ==  typeof(DataGridComboBoxColumn))
        {
            var editor = e.EditingElement;
            editor.PreviewKeyDown += Editor_PreviewKeyDown;
        }
    }
    private void Editor_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        bool itmFound = false;
        var cmb = sender as C1ComboBox;
        foreach (Country cntry in cmb.Items)
        {
            if(cntry.Name.StartsWith(e.Key.ToString()))
            {
                itmFound = true;
                break;
            }
        }
        if(!itmFound)
        {
            MessageBox.Show("No option starts with the entered letter");
        }
    }
    

    Best wishes,

    Ruchir

    C1DataGrid_ComboCols.zip

  • Posted 11 December 2019, 12:44 am EST

    Hi Ruchir,

    Thank you for the response. Your code is very close to what I am looking for. I tried to modify your code but couldn’t do much. So, I am back to you.

    Actually, I need to compare more characters instead of just the first one.

    Example: If one of the item in the combobox is say “Apple”. User types in “App” nothing happens because it is a match. Now user presses ‘x’ from the keyboard, This causes a mismatch because there is no item that begins with “Appx”. Now I want to show the message “item not in list”. Hope I was able to explain it well.

    Regards

  • Posted 12 December 2019, 12:38 am EST - Updated 4 October 2022, 12:00 am EST

    Hi Anil,

    Yes, I understand.

    To meet your requirement I have modified the earlier attached application and attached here for your reference. Now, the behavior is as shown in the attached video.

    If there is anything you need, do let me know.

    Best wishes,

    Ruchir

    C1DataGrid_ComboCols_Modified.zip

  • Posted 12 December 2019, 4:19 pm EST

    Hi Ruchir,

    That’s exactly what I was looking for. In fact, the idea to remove the last unmatching character is the icing on the cake. Thanks a lot. You guys are awesome.

    Regards,

    Anil

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels