Setting the GridView Selected item to none - c#

I am developing windows 8.1 app using Xaml and c#, Windows 8.1 selectes the first list item when the list loads. How can i Change this so that the list has no selectedItem when it loads.

From MSDN documentation:
Use the SelectedIndex property to determine the index of the currently selected row in a >GridView control. You can also use this property to programmatically select a row in the >control. To clear the selection of a row, set this property to -1.
Source: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindex(v=vs.110).aspx

In my case, setting the SelectedIndex to -1 did not work. If you are populating your GridView from a CollectionViewSource, you have to insert this line in the GridView :
IsSynchronizedWithCurrentItem="False"
From MSDN
Selection behavior and CollectionViewSource
List controls that derive from Selector have a default selection behavior that depends on what the items source is (the type that's used for ItemsSource). If the items source is a CollectionViewSource instance, then the behavior in the selection control is that the selection will default to the current item. When the list is first displayed, the selection defaults to the first item as current item. If you don't want the first item to be selected in this case, set IsSynchronizedWithCurrentItem to false in the GridView.

Related

could not load all results for selected value in asp.net

How to modify the items in listbox when it is databound.
suppose i have 1 listbox and 1 combobox.i want to display results in listbox only for the value selected in combobox and remove the other value in listbox in windows form in asp.net.
I think you should add an OnSelectedIndexChanged to your combobox and set AutoPostBack="True"
Then selecting an item should trigger a post back and call the method you set for the OnSelectedIndexChanged. There you can fill the ListBox with values and remove the selected item from the ListBox too.

DevExpress DataGrid filter cell triggers SelectedItem change

I have a DevExpress WPF datagrid and the SelectedItem is used via Binding in the ViewModel.
When I select a row, it loads data in a detail view.
However, when I select a filter cell, it also triggers the SelectedItem binding and it tries to load...well, nothing as there is no real row selected.
I intend to use the filter as the following: the SelectedItem is the first item in the filtered list.
How can I prevent the filter cell from triggering the SelectedItem change and make my first item as the selected one?
You need to set the SelectedItem's Binding with an
UpdateSourceTrigger=PropertyChanged
the default UpdateSourceTrigger for all binding's inside a DataGrid is LostFocus.
that's why your item is updated when leaving the row , it lost it's focus.
Please check syntax since it was not written in VisualStudio .
<ComboBox SelectedItem="{Binding YourItem,UpdateSourceTrigger=PropertyChanged}" />

C# Winforms - How can I dynamically set the selectedItem of combo box?

I cannot seem to figure out how to dynamically change the selected item in a combo box. I am trying this:
myComboBox.SelectedItem = item.Id;
Here item.Id is a int that corresponds to valid ValueMember that is bound to the combobox. However the combobox remains unchanged. I have trying invalidating the control after changing the selected item. What's the trick?
Thanks
try SelectedValue instead..
myComboBox.SelectedValue = item.Id;
You can use either of these:
SelectedIndex Property
ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.
SelectedItem Property
Get or Set A zero-based index of the for selected item.

Problematic comboBox Windows Forms

I use VS2008 C# + Windows Forms. I can't understand why comboBox does not behave the way it should.
In Design mode, I added a comboBox to my form, and edit Items to add "A" and "B". Double-clicking brings me to SelectedIndexChanged event, which I edit to display the selected text with MessageBox.
private void comboBoxImageSet_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBoxImageSet.SelectedText);
}
When I run, and select "A" or "B" in the comboBox, the MessageBox appears, but nothing is written.
Why?
Thanks.
Here the differences between the selection properties of a ComboBox control.
SelectedIndex;
SelectedItem;
SelectedText;
SelectedValue.
The SelectedIndex property :
Gets or sets the index specifying the currently selected item.
Simply indicates the index of the selected item in the selection list. (Information provided for your kind information only. =))
The SelectedItem property :
Gets or sets currently selected item in the ComboBox.
The SelectedItem represents the element that is currently selected as per the ListControl of the ComboBox. That is why this is what you want to use, to answer your question.
The SelectedText property :
Gets or sets the text that is selected in the editable portion of a ComboBox.
That is, when you edit the TextBox portion of the ComboBox, the text that might be selected when you enter for edit, or any other type of text selection. This indeed does include any selection made through the ListControl portion of the ComboBox. For instance, if your ComboBox.DropDownStyle property is set to ComboBoxStyle.DropDownList, then you will never be able to select any text in the editable portion of the ComboBox. Despite, you're able to select another item within the its list. That is why it is not the right property to use to serve your purpose.
The SelectedValue property :
Gets or sets the value of the member property specified by the ValueMember property.
Only used when using DataBinding, in conjunction with the DisplayMember property. For instance, when you want to display the name of a customer, and select him by his database Id, then the DisplayMember should display the customer's name, and the ValueMember the Id. This way, when you select one customer, the SelectedValue changes and raises the SelectedValueChanged event inherited from the ListControl. (Information provided for your kind information only. =))
The SelectedText property returns the text that is marked in the combobox, not the selected item. If the combobox is editable you can mark a part of the text and the SelectedText property will return the marked text. Look here.
What you are interested in is the SelectedItem property or the SelectedValue property.
ComboBox.SelectedText
A string that represents the currently
selected text in the combo box. If
DropDownStyle is set to DropDownList,
the return value is an empty string
("").
Use SelectedItem instead of SelectedText
SelectedText:
Gets or sets the text that is selected
in the editable portion of a ComboBox.
That is, it gets the text that is currently marked.
You want to use SelectedItem.ToString().

Retrieve value of most recently SelectedItem from multi-select listbox

How can I retrieve the value of a clicked item in a multi-select listbox?
Currently, if I click one item and use lstBox.SelectedValue, it will return the proper value; however, if I then click on another item, I am still shown the first item's value.
Basically, I want the value of the item most recently clicked on, regardless of whether or not it is the SelectedValue.
If it is a multiple selection listbox, you can get a collection of all the selected items by using SelectedItems instead of SelectedItem.
If you need to know the sequence in which the items were selected, or which was selected most recently, I think you would need to record if yourself by SelectedIndexChanged event.
The SelectedIndexChanged handler will get called when you select/unselect an item in the listbox.
However, it doesn't indicate which one was selected/unselected.
listbox1.SelectedItems
will contain the currently selected items and you could internally keep track of which index was most recently added.

Categories