GridComboBoxColumn

I have a GridComboBoxColumn and bind it to a source. I want the combobox to have a blank item without modifying my actual source. What is the best way to achieve this?

9 Replies

MA Mohanram Anbukkarasu Syncfusion Team November 20, 2020 08:50 AM UTC

Hi Carlin, 

Thanks for contacting Syncfusion support.  

You can achieve your requirement to add an empty item to the ComboBox by using GridTemplateColumn instead of using GridComboBoxColumn as shown in the following code example.  

Code example :  

<syncfusion:GridTemplateColumn MappingName="Country"> 
    <syncfusion:GridTemplateColumn.CellTemplate> 
        <DataTemplate> 
            <TextBlock Text="{Binding Country}" /> 
        </DataTemplate> 
    </syncfusion:GridTemplateColumn.CellTemplate> 
 
    <syncfusion:GridTemplateColumn.EditTemplate> 
        <DataTemplate> 
            <ComboBox SelectedItem="{Binding Country, Mode=TwoWay}"> 
                <ComboBox.ItemsSource> 
                    <CompositeCollection> 
                        <ComboBoxItem IsEnabled="False" Content=""/> 
                        <CollectionContainer Collection="{Binding Source={StaticResource ComboBoxItems}}" /> 
                    </CompositeCollection> 
                </ComboBox.ItemsSource> 
            </ComboBox> 
        </DataTemplate> 
    </syncfusion:GridTemplateColumn.EditTemplate> 
</syncfusion:GridTemplateColumn> 

We have prepared a sample using the above given code example and it is available in the following link for your reference  


Please revert to us with details if we have misunderstood your requirement.  

Regards, 
Mohanram A. 





CS Carlin Smith November 20, 2020 04:51 PM UTC

I do not like your solution because it requires a lot of extra code to achieve. I would like your solution more if I could put it into a ResourceDictionary xaml file but the way you are doing binding on it makes that really sticky?

This works correctly but has no additional item:
                    <sync:GridComboBoxColumn HorizontalHeaderContentAlignment="Left" MappingName="User" Width="150">
                        <sync:GridComboBoxColumn.ItemsSource>
                            <Binding Path="UserList.Items"/>
                        </sync:GridComboBoxColumn.ItemsSource>
                    </sync:GridComboBoxColumn>


This does not work, it only adds the "AdditionalItem" and fails to add my UserList.Items:
                    <sync:GridComboBoxColumn HorizontalHeaderContentAlignment="Left" MappingName="User" Width="150">
                        <sync:GridComboBoxColumn.ItemsSource>
                            <CompositeCollection>
                                <ComboBoxItem IsEnabled="True" Content="AdditionalItem" />
                                <CollectionContainer Collection="{Binding UserList.Items}" />
                            </CompositeCollection>
                        </sync:GridComboBoxColumn.ItemsSource>
                    </sync:GridComboBoxColumn>


Why does the CompositeCollection not work?


CS Carlin Smith November 20, 2020 06:30 PM UTC

also, i need to be able to select the blank item to deselect all values


CS Carlin Smith November 20, 2020 06:53 PM UTC

I would like to request that you add 2 properties to GridComboBoxColumn

AddBlankItem="True"

CanSelectBlankItem="True"
or
CanDeselect="True"

and if the combobox has focus, if user presses delete key, the item is deselected


I've been developing using your controls for several months now and i am getting a bit fatigued by them.

It is really nice that we can customize the controls to work exactly the way we'd like them to. However, the kinds of functionality we need from your controls is actually really basic stuff and should be built in already.

I use your controls to avoid having to get crazy with WPF code and possibly code-behind to achieve simple tasks. These simple tasks should be built into your controls.

A lot of the things my company desires to do are not unreasonable and are really really basic stuff. It appears that the above need is a SUPER COMMON problem that WPF programmers come across. The need to have a blank item in the list and the ability to select that blank item to deselect altogether.

Microsoft's implementation of the combobox is not very good and requires customzations that shouldn't be necessary if they had done it right or developed the control fully. Microsoft left things incomplete on the control that are commonly needed and are a pain to program around.

This is where Syncfusion should come in and fix that for us. Provide that basic functionality that Microsoft does not provide. Make our lives easier. Speed up development for its customers. We chose Syncfusion because we wanted some tools that would speed up development and make things easier overall.


MA Mohanram Anbukkarasu Syncfusion Team November 23, 2020 12:47 PM UTC

Hi Carlin, 

Thanks for the update. 

Query 
Solution 
This works correctly but has no additional item: 
                    <sync:GridComboBoxColumn HorizontalHeaderContentAlignment="Left" MappingName="User" Width="150"> 
                        <sync:GridComboBoxColumn.ItemsSource> 
                            <Binding Path="UserList.Items"/> 
                        </sync:GridComboBoxColumn.ItemsSource> 
                    </sync:GridComboBoxColumn> 


This does not work, it only adds the "AdditionalItem" and fails to add my UserList.Items: 
                    <sync:GridComboBoxColumn HorizontalHeaderContentAlignment="Left" MappingName="User" Width="150"> 
                        <sync:GridComboBoxColumn.ItemsSource> 
                            <CompositeCollection> 
                                <ComboBoxItem IsEnabled="True" Content="AdditionalItem" /> 
                                <CollectionContainer Collection="{Binding UserList.Items}" /> 
                            </CompositeCollection> 
                        </sync:GridComboBoxColumn.ItemsSource> 
                    </sync:GridComboBoxColumn> 


Why does the CompositeCollection not work? 

You should not bind the items source collection to the CollectionContainer. You have to make use of the CollectionViewSource as shown in the following code example.  

Code example:  

<Window.Resources> 
     <CollectionViewSource x:Key="ComboBoxItems" Source="{Binding Countries}" /> 
 </Window.Resources> 
 
<syncfusion:GridComboBoxColumn MappingName="Country"> 
    <syncfusion:GridComboBoxColumn.ItemsSource> 
        <CompositeCollection> 
            <ComboBoxItem Content="Blank"/> 
            <CollectionContainer Collection="{Binding Source={StaticResource ComboBoxItems}}" /> 
        </CompositeCollection> 
    </syncfusion:GridComboBoxColumn.ItemsSource> 
</syncfusion:GridComboBoxColumn> 


also, i need to be able to select the blank item to deselect all values 
This can be achieved by creating a custom combo box cell render as shown in the following code example.  

Code example :  

this.dataGrid.CellRenderers["ComboBox"] = new CustomComboBoxCellRenderer(); 
 
public class CustomComboBoxCellRenderer : GridCellComboBoxRenderer 
 { 
     public override void OnInitializeEditElement(DataColumnBase dataColumn, ComboBox uiElement, object dataContext) 
     { 
         uiElement.SelectionChanged += ComboBox_SelectionChanged; 
         base.OnInitializeEditElement(dataColumn, uiElement, dataContext); 
     } 
 
     private void ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) 
     { 
         if (e.AddedItems.Count == 1 &&  
             e.AddedItems[0] is ComboBoxItem && (e.AddedItems[0] as ComboBoxItem).Content.ToString() == "Blank") 
             (sender as ComboBox).SelectedItem = null; 
     } 
 } 






I would like to request that you add 2 properties to GridComboBoxColumn 
AddBlankItem="True" 

CanSelectBlankItem="True" 
or 
CanDeselect="True" 
We have forwarded this query to our development team for analysis. We will update with further details on 25th November 2020. We appreciate your patience until then.  




Please let us know if you have any concerns in this.  

Regards, 
Mohanram A. 



MA Mohanram Anbukkarasu Syncfusion Team November 25, 2020 11:06 AM UTC

Hi Carlin, 

Sorry for the inconvenience.  

We are able to understand the need for the property AddBlankItem to add an empty item to the ComboBox without changing the items source. But we are little unclear with CanSelectBlankItem and CanDeselect properties you have asked for. Kindly provide more details about the need for these properties. So that we can check with the corresponding team and update with further details.  

Regards, 
Mohanram A. 



CS Carlin Smith November 30, 2020 09:06 PM UTC

Mohanram,

Thanks for your follow up.

Scenario 1:

There are scenarios where you want a blank item to be selected by default in your grid combobox. Once they have selected an actual item from the list in your comobox, they SHOULD NOT be able to select that blank item again.

 

Scenario 2:

There are other scenarios where you want a blank item to be selected by default in your grid combo box.  Once they have selected an actual item from the list in your combobox, they SHOULD be able to select that blank item again.  If they select the blank item, it sets the mapped property to null.

 

Let me know if you need additional clarification.

 

Thanks,

Carlin



MA Mohanram Anbukkarasu Syncfusion Team December 1, 2020 02:00 PM UTC

Hi Carlin, 

Thanks for the update.  

We have forwarded this to the corresponding team. We will update with further details on December 3, 2020. We appreciate your patience until then.  

Regards, 
Mohanram A. 



MA Mohanram Anbukkarasu Syncfusion Team December 3, 2020 02:06 PM UTC

Hi Carlin, 

Thanks for your patience.  

We have checked this with the corresponding team. New features can be considered to implement only based on the number of users requests. We have provided extensible support to achieve such requirements with application level changes. You have to make use of the workaround provided in our earlier updates. Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon