Bind WPF ComboBox to database record - wpf

I've got and update form that I've bound to a datatable in WPF. I've got one Field in the Record STATUS that has only 3 possibilitye A for Archived, C for Current or D for Draft. I would like to put a Combobox on the form that would show the text for the selected record and can Update the database if the user wants to change the status. I have searched all over the web and have not found an easy way to do this. Can someone out there give me a suggestion as to where to start?

I didn't get any responses. Reading it I didn't phrase the question very well. But I found an answer to my question in another post.
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding Path=STATUS}">
<ComboBoxItem Tag="A">Archived</ComboBoxItem>
<ComboBoxItem Tag="C">Current</ComboBoxItem>
<ComboBoxItem Tag="D">Draft</ComboBoxItem>
</ComboBox>
If anybody else is having this problem this might help.

Related

Object reference not set to an instance of an object in WPF Combo box [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
When try to access newly added value in the combo box it gives this error message!
Object reference not set to an instance of an object
window have one combo box and text box! when click the Add new item button, text box value should be loaded in to the combo box.! This loaded action working fine! but when we try to access the newly added one( for example when click edit button) , above error message displayed.
XAML Code:
<ComboBox x:Name="cmbList" x:FieldModifier="public" HorizontalAlignment="Left" Margin="58,10,0,0" VerticalAlignment="Top" Width="147"
<ComboBoxItem Content="1st Item"/>
<ComboBoxItem Content="2nd Item"/>
<ComboBoxItem Content="3rd Item"/>
</ComboBox>
Button Click function :
cmbList.Items.Add(textbox1.text)
Up to this point it working properly! If I try to re select it gives this error message!
Please anyone correct me!
Thanks in Advance!
You are adding a String to a collection of ComboBoxItems and therefore the ComboBox or your editing code won't be happy as I imagine you are expecting a ComboBoxItem.
Try replacing this line in your click event:
cmbList.Items.Add(textbox1.text)
With this:
cmbList.Items.Add(new ComboBoxItem { Content = textbox1.text });

Bind Combobox with huge data in WPF

I am trying to bind combobox with custom object list. My object list have around 15K record and combobox takes long time to show the data after clicking on combobox.
Below are the code:
<ComboBox Height="23" Name="comboBox1" Width="120" DisplayMemberPath="EmpName" SelectedValue="EmpID" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"/>
code behind:
List<EmployeeBE> allEmployee = new List<EmployeeBE>();
allEmployee = EmployeeBO.GetEmployeeAll();
comboBox1.ItemsSource = allEmployee;
allEmployee have around 15K record.
Can any one Suggest how can I improve my combobox performance?
That's bad UI design: No user will read through 15K records.
You can improve the performance by allowing the user to enter some filter criteria before showing the results, for example, by using an AutoCompleteBox instead of a ComboBox.
You could try a VirtualizingStackPanel as described here - http://vbcity.com/blogs/xtab/archive/2009/12/15/wpf-using-a-virtualizingstackpanel-to-improve-combobox-performance.aspx
As others have said, you really want to re-imagine your UI, as a ComboBox isn't appropriate for 15k records.
Try using a VirtualizingStackPanel as ItemsPanel for the ComboBox.
<ItemsPanelTemplate x:Key="ComboBoxItemsPanelTemplate">
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
<ComboBox ItemsPanel="{StaticResource ItemsTemplate}"/>

ComboBox IsEditable Behavior question WPF

<ComboBox IsEditable="True" SelectedItem="{Binding}">
<ComboBoxItem>Angus/ComboBoxItem>
<ComboBoxItem>Angie/ComboBoxItem>
<ComboBoxItem>Jane</ComboBoxItem>
<ComboBoxItem>Steve</ComboBoxItem>
</ComboBox>
I would like to allow the user to find their selection by typing in a name, so I have set IsEditable equal to true.
Now my question regarding behavior is this:
When I start typing Ang...I would expect to see BOTH Angus and Angie in the dropdown....however, I only see Angie first and I dont see Angus until I enter Angu ... .
Can we replicate this behavior like in google search autocomplete box.?
Thanks!
IMO you can't do it with the regular ComboBox, but the good news are:
There's an excellent autoCompleteBox in the WPF toolkit.
this is an example how to use it:
http://www.c-sharpcorner.com/UploadFile/dpatra/537/

using wpf datagridcomboboxcolumn's IsSynchronizedWithCurrentItem

(see below for my own answer that I came up with after letting this percolate for days & days)
I am trying to achieve the following scenario in WPF.
I have a datagrid that is displaying rows of data for viewing and additional data entry. It is a new app but there is legacy data.
One particular field in the past has had data randomly entered into it. Now we want to limit that field's values to a particular list. So I'm using a DataGridComboBoxColumn. FWIW I have alternatively tried this with a DataGridTemplateColumn containing a ComboBox.
At runtime, if the existing value is not on the list, I want it to display anyway. I just cannot seem to get that to happen. While I have attempted a vast array of solutions (all failures) here is the one that is most logical as a starting point.
The list of values for the drop down are defined in a windows resource called "months".
<DataGridComboBoxColumn x:Name="frequencyCombo" MinWidth="100" Header="Frequency"
ItemsSource="{Binding Source={StaticResource months}}"
SelectedValueBinding="{Binding Path=Frequency,UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
What is happening is that if a value is not on the list then the display is blank. I have verified at runtime that the IsSynchronizedWithCurrentItem element is indeed False. It is just not doing what I am expecting.
Perhaps I am just going down the wrong path here. Maybe I need to use a textbox in combination with the combobox. Maybe I need to write some code, not just XAML. I have spent hours trying different things and would be really appreciative of a solution. I have had a few suggestions to use this class or that control but without explanation of how to use it.
Thanks a bunch!
I have finally solved this.
The trick is to get rid of the comboboxcolumn and use a template that has a textbox for display and a combobox for editing. However, I still spent hours with a new problem...when making a selection in the combobox, it would modify any other rows where I had also used the combobox in the grid. Guess what solved the problem! The IsSynchronizedWithCurrentItem property that I was trying to use before. :)
<DataGridTemplateColumn x:Name="frequencyCombo" Header="Frequency">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Frequency}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding Source={StaticResource frequencyViewSource},
TargetNullValue=''}"
SelectedItem="{Binding Path=Frequency}" IsSynchronizedWithCurrentItem="False"
/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
No ugly hacks. No non-usable choices hanging around at the bottom of the dropdown. No code to add those extra values and then clean them up.
I am not going to take away the "Answer" on Mark's suggestion since it enabled me to get the app into my client's hands, but this is the solution I was looking for. I found it buried in a "connect" item after hours of web searching.
Thanks for everyones help!
Could you please clarify what's happening here? It's unclear what the "existing value" is at runtime - if this is the field where data is being randomly entered, by limiting it does this mean you're running some sort of validation logic although you still want it to display?
Also, I'm more on the Silverlight side of things...does WPF also default to one way binding?
Rather than mixing the static resource and the view model property as a source for items on the list, have you tried using an ObservableCollection or CollectionViewSource in the view model? Then you could insert and remove the non-standard items at will and make them selected (or not) whenever you want. So the "normal" list would have the normal months, but when an odd one comes along, add that to the list and make it selected. Seems like it would be easier to control exclusively in the view model.
Why not just do something like:
//create collection
PagedCollectionView view = new PagedCollectionView(e.Result);
view.SortDescriptions.Add(
new SortDescription("Months", ListSortDirection.Ascending));
gridProducts.ItemsSource = view;
//filter collection by category
PagedCollectionView view = new PagedCollectionView(e.Result);
view.Filter = delegate(object filterObject)
{
Product product = (Product)filterObject;
return (product.CategoryName == "Legacy");
};
gridProducts.ItemsSource = view;
//create categories through grouping
PagedCollectionView view = new PagedCollectionView(e.Result);
view.GroupDescriptions.Add(new PropertyGroupDescription("Legacy"));
view.GroupDescriptions.Add(new PropertyGroupDescription("etc..."));
gridProducts.ItemsSource = view;
Try this:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Months}"
Text="{Binding Value}"
IsEditable="True" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Want to add the selectedValue also in the xaml along with the selected Text in my wpf window

I have a compbobox with the following information
<ComboBox
Margin="10,10,0,13" Name="ComboBox1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="128" Height="21">
<ComboBoxItem Content="All " ></ComboBoxItem>
<ComboBoxItem Content="Printed"></ComboBoxItem>
<ComboBoxItem Content="Unprinted"></ComboBoxItem>
</ComboBox>
I want to also add the selected value for all being 2 printed being 1 and unprinted being 0 is there a way to add it in the xaml?
If you reversed the order of your ComboBoxItem declarations, then the property SelectedIndex on the ComboBox would yield these results.
Though its not a very scalable solution. You might want to try creating a collection of enumerated types and then binding that to the ItemsSource of the ComboBox and then investigating the SelectedItem to see which item has been selected.
Hope it helps.
I think I found the answer
Selected Value will then work because it will retrieve whatever is in Tag of the selected item.

Resources