C# Combo DataBinding to Empty Guid shows 00000000-0000-0000-0000-000000000000 - c#

Hi I have windows form with a Combo filled with 3 options where their values are Guid's and that has a DataBinding to a selected object.
EmployerMemberDefault item = EmployerMemberDefault();
item.GroupUid = Guid.Empty;
cbGroupEmployer.DataBindings.Clear();
cbGroupEmployer.DataBindings.Add("Value", item, "GroupUid");
When I create a new object the property that is databinded to the combo is set to Guid.Empty. If I compile and run, when I create that new object I see "00000000-0000-0000-0000-000000000000" in the combo. Is there any way I can see empty text in the combo instead of the Guid.Empty value?
Thanks.

Change the source property from Guid to Guid?/Nullable<Guid> I haven't done this with Guid, but it works with int/DateTime and other types that do not allow null.

"00000000-0000-0000-0000-000000000000" is what Guid.ToString returns. To show something different you'd have to translate that value in some way--i.e. bind to something else that stores the guid and translates it.

Related

Get value of umbraco datatype in code

I have created a checkbox list type of data type in umbraco. see below screen shot
I am able to get the text value of this data type which is "All" in this case. But I am unable to get the Value in code.
umbraco.library.IProperty property = node.GetProperty("countries");
string val = property.Value;
I also looked at all the property fields but Value of this is not available.
Any one has any idea
I am using Umbraco 6.2.4
Looks like you should be using the GetPreValueAsString for your soultion.
string val = umbraco.library.GetPreValueAsString(node.GetProperty<int>("countries"));

Multiply value selected in combobox by a number in label

I am trying to work out how to multiply a selected value in a ComboBox by a string value stored in a label. I have tried converting both values to ints but this error keeps appearing:
Unable to cast object of type 'System.Windows.Controls.ComboBoxItem' to type 'System.IConvertible'.
int quantity = Convert.ToInt32(comboBox3.SelectedItem);
int price = Convert.ToInt32(label1.Content);
label2.Content = quantity*price;
Many Thanks
I don't quite remember which exact property of ComboBoxItem it was (I think Text), but you need to get the value of its string rather than the whole object to perform the conversion. After that, just use int's ParseString class method.
You want the SelectedItem's Content, not the SelectedItem itself (assuming WPF):
Convert.ToInt32(comboBox3.SelectedItem.Content);
But if you have set SelectedValuePath and are using some data binding to a model, you can get the selected value in an easier way (without even using convert, if the model is an integer):
comboBox3.SelectedValue

"Predict" next model ID

Sheesh...
I think this can be done but I just can't figure it out at the moment.
So I've created this app, that's working fine. But for some reason (too long to explain sorry) I would need to predict next ID to come from data table.
Note that last id + 1 will not work. I've tried this.
var lastProperty = db.Properties.OrderByDescending(p => p.PropertyID).FirstOrDefault();
int propID;
if (lastProperty == null)
{
propID = 1;
}
else
{
propID = 1 + lastProperty.PropertyID;
}
And as long as properties don't get deleted it works...
As soon as one is deleted, it messes up of course, since lets say we delete 6th Property,
last one will be 5th now, and with that code we'll get 6 5(last one) + 1, and I save my model related to Property with PropertyID 6 which I got from that code, and next Property will be 7 since database still remembers that 6 existed and was deleted... Model I intended to have same PropertyID as THAT last Property will not have it, and it'll fail...
Also, I can't add this AFTER saving Property, I realize that might seem as a solution but it's not. It has to be predicted.
Thank you... Please help...
UPDATE
This is the use case I'm trying to accomplish.
Property model with ID Name DataType properties.
List model with ID ListValue PropertyID properties.
When new Property is created user types in the Name, and from premade dropdown list for DataType selects a value, if that value is List, it opens additional form that contains a listbox, textbox and a button (add_Button). User types in the value in a textbox, and clicks the add_Button to add it to List.
Listbox is populated from List model, and add_Button, saves values from textbox to ListValue property of List, also as you've seen, I'm trying to manually add PropertyID to List by predicting it's value...
Then, upon finishing adding all the elements wanted through textbox, by clicking Create button, Property is then saved.
Hope this was clear enough.
It seems like you want to know the current state of the identity column:
SELECT IDENT_CURRENT('Table')
When need to know what your primary key values are going to be before committing to the Db you could use a uniqueidentifier (GUID) as the Pk for your Property and therefore as the Fk for your List entity. That way you can create it your self and it'll always be unique on committal.
You mention Entity Framework, have you tried adjusting your model?
StoreGeneratedPattern can be adjusted to allow automatic update of your local model when you store to the database.

"All" value for a Combobox Data Filter in WPF

During the construction of user control I populate the combobox with some data. Works fine. I have Status.ID as valuePath and Status.Name as displayPath.
cmb.ItemsSource = dbEntities.Status
The comobox will be used as a filter control and I need to insert some value for "All", which will be used as the empty filter.
First I tried a funny solution:
ObjectSet objectSet= dbEntities.Status;
Status stAll = new Status();
stAll.ID = -1;
stAll.Name = "All";
objectSet.AddObject(stAll);
cmb.ItemsSource = objectSet;
For some reason the object is not added to the objectSet. It didnt throw any exception either.
Then I tried to insert it manually to the first index but I got the error:
"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
My code looked like:
cmb.ItemsSource = entities.Status;
cmb.Items.Insert(0,"All");
Both didnt work. What would be the easiest way to add that line to the combobox? The error message got me confused. I am not sure how to use the ItemsSource for such a purpose.
edit: I did not have enough rep to answer my own question so here is the working code. Thanks Craig again.
CompositeCollection comp = new CompositeCollection();
comp.Add(new CollectionContainer {Collection = dbEntities.Status});
Status stAll = new Status();
stAll.ID = -1;
stAll.Name = "All";
comp.add(stAll);
cmb.ItemsSource = comp;
//do whatever filter you want when the selected value is -1
There are a couple different problems with what you are trying to do. You can't manipulate the Items when you are using the ItemsSource, instead you have to go through the object that is set to the ItemsSource. That is what the error message is about in the second part. Its because when you set the ItemsSource the Items value is unused it is not populated with the values of the ItemsSource.
I'm not familiar enough with the ObjectSet class to know why the first case is not working. However, it seems awkward to add an item to your values you are pulling from somewhere else, just to have the all case. The better solution is to use a null value to represent nothing. Unfortunately, there is no built in way to do this in WPF. However, there is a fairly easy solution of using an Adaptor do do this. I have used this solution a NullItemSelectorAdaptor, that enables null as a selection even if null is not in the list. All you have to do is wrap your combobox in the NullItemSelectorAdapter and null will be added as a value. The blog post explains everything pretty clearly, so I won't repeat it. You than can setup your filter so that null equates to no filtering.

How to insert a Combobox value in to a database

How to insert a Combobox value in to a database. When i hit Save button in my application, it will stored stored into database like this way..
System.Windows.Controls.ComboBoxItem
What i do for getting correct value of a combo box?
if you are using a string in the combobox, then you can use the example that Akash Kava showed.
Also, if the combobox contains other elements, you can get those elements through the use of something like
((ComboBoxItem)rpcombo.SelectedItem).Content.ToString()
and casting it to what you need.
lets say your combobox has string items what you need is like this
froeach( ComboBoxItem item in ComboBox1.Items )
{
string str = (str)item;
// saving value
}
Best Regards,
Iordan
This question seems a bit too broad. We are going to need nore information about this to be able to answer this one.
What kind of database are you using?
Are you connected to the database or do you need help with tahat as well?
In that case there are alot of good reading on the internet to your aid, that I can link to.
Do you want to save the ComboBoxItem as an .net object or det values of the item?
The list can go on, you have to give me something more to chew on.
MyObject obj = myComboBox.SelectedItem as MyObject;
obj.ValueIamInterestedIn; <--- this should be inserted
or if you have string or any valuetype array as ItemsSource then,
myComboBox.SelectedItem.ToString();

Categories