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();
Related
Seems like a really simple thing, but all of the research and everything I have tried just doesn't seem to work. I am using VS2013 and .Net 4.5
I have a 2 column WPF DataGrid that isn't using databinding. The grid is already populated with data from a SQLCE table and I want to select a row and return as a string the data in the cell of column 0 of the selected row.
I can see that the data is there in the debug watch window
I just don't know how to access that value so that I can use it.
private void dgPickList_MouseDoubleClick( object sender, MouseButtonEventArgs e )
{
selectItem();
this.Close();
}
private void selectItem()
{
_pickedItem = dgPickList.SelectedItem.ToString();
}
This code obviously doesn't work so well but it does let me see the cell data in a Non-Public members _values field.
I really appreciate any help.
Jeff
Cast the SelectedItem back to the original type you populated the grid with.
For example, if you use a DataTable, cast to a DataRow and grab the first column (here I'm assuming it's an integer type that represents a unique ID):
var id = ((DataRow)dgPickList.SelectedItem).Field<int>(0);
If you're using a collection of a custom class, something like this should work:
var id = ((MyClass)dgPickList.SelectedItem).Id;
use DataGrid.SelectedCells instead. This will return a collection of selected cells in which you can find the column you need to read.
eg:
var celldata=dataGrid1.SelectedCells[0];
var val=celldata.Column.GetCellContent(celldata.Item);
So I have an arraylist that will display data to a listbox. Then, the user can select an entry in the listbox. From there, I need to be able to grab whatever entry they select. I'm using VS 2010.
I have been trying
tempArray.Insert(0, myarray.IndexOf(mylistbox.SelectedIndex);
All this is doing is giving me the actual index number and not the contents of the index. I'm not sure how to index the arraylist to get the object that is contained at that index.
And yes, I know that I should be using List objects, but it is for class and we have yet to be taught list objects.
As the name suggests .IndexOf() will return an index from the array.
Array.IndexOf Method
This is obviously not what you want.
What you should look at is using the SelectedItem instead of the SelectedIndex.
That way, you can access the object directly and insert it into your list. One thing to remember is that SelectedItem will return an Object. This means that it will have to be cast to the type that you expect to be using.
Also, are you wanting to constantly insert items to the top of the list or does it not matter. If you can append it to the end of the list, try using .Add(yourObject).
If these are actual arrays you are working with, shouldn't you be able to to access what's in the array by using the [ ] syntax? i.e.
myArray[myListBox.SelectedIndex]
1) Dont use ArrayList... that's an old class. Use List<T> instead.
2) Have you tried ListBox.SelectedItem ? That seems a bit simpler...
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.
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.
Hello I have an object which has value "B11" i.e
object data = "B11";
i have a variable called string resultCell. How do I get B11 into resultCell.
it doesn't seem to work for me in c#. I have tried things like ....
resultCell = (string)data;
resultCell = Convert.ToString(data);
More information
I am using excel services to get a value in a cell. the excel services returns a object[]. Its a single element though. I want the value in a string. i.e. B11 in the object as string.
Much of the time, calling data.ToString() will work. Without the specific type of the data object, there isn't any way to answer the question.
Is the value stored in a property of data?
Edit: Could you try (string)object[i], where i is the index of the particular piece of data you want to retrieve? If you need every value in the array, you'll probably want to use a foreach loop.
i think you need to just assign value as data.ToString() to cell.
hope this work for you.