list view selection mode none in windows form applications - c#

i am working on windows form application ..i have a list view.
in the list view some time i will select some row then i will click add button .that time selected rows will add to list view.
after that i want to make my listview selection mode none so i wrote code like this
List_Item.Select = Nothing ..
but this code throwing error? how i can make my list view selection mode none?
any help is very appreciable.thanks in advance

loop every item and set the selected field to false
For Each item As ListViewItem In Me.List_Item.Items
item.Selected = false
Next

Related

DropDownList Showing First Item Empty

Every time this Dropdown is showing first Item Blank
ComboBox cb;
List<string> namesCollection=new List<string>();
namesCollection.Add("---- Select ----");
namesCollection.Add("ABC1");
namesCollection.Add("ABC2");
namesCollection.Add("ABC3");
namesCollection.Add("ABC4");
foreach(string pname in namesCollection)
cb.Items.Add(pname);
Does anyone have solution for this ?
You appear to be defining the ComboBox right there in your code, so I'll assume it's actually displayed somewhere in your form / window.
It's normal for the ComboBox to display a blank line initially.
Specify the item you want to display, immediately after populating the ComboBox with data:
cb.SelectedIndex = 0;
You probably have a line feed in your items collection. Hard to see. Adding a cb.Items.Clear() before populating is probably a good idea anyway and will get rid of the problem, if you can't locate it.

Listbox selecting first item again wpf mvvm not working

Consider a listbox in WPF (MVVM) having a list of items(items are file names).If First item in listbox selected then the first file will be opened correctly. Suppose, if 'New' button(to open new file)is clicked and new file is opened. Now, if the first item(first file) is selected then fist file is not opening insetead new is only opened because list box selection is not changed. Instead if any other item is selected then it is working fine.How to make the first item to get selected again.
You can set your selectedItem to null then set it back to your value that you need.
Add an extra item as "SelectFile" in databound collection of ListBox.
Whenenver Newbutton get clicked, through button command execution, set ListBox selected item as "SelectFile".
This will allow you to reselect last file.
If this default entry "SelectFile" get selected, do not proceed with file opening from view model.

prevent item losing focus when refreshing list view in c#

I have list view and some columns "ID", "product name" and "Vendor". I can add, edit or delete those items and after "messing up" with items list view is refreshed something like this.
listView.Items.Clear();
foreach (var item in result)
{
ListViewItem lv = new ListViewItem(item.ID.ToString());
lv.SubItems.Add(item.Name.ToString());
lv.SubItems.Add(item.Vendor.ToString());
listView.Items.Add(lv);
}
so this method works fine except every time i refresh list focus of item is lost, which is logical because i deleted all items from list and filled it again.
So my problem is how can i keep focus on edited and newly added items, primary when i edit some items i would like listview not to scroll to the top but to stay on place where that edited item was.
I tried with method FindItemWithText and than setting focused items of listview to item that is found but it didn't work.
Is there ant solution to this kind of problem?
Save focused item's index to the variable:
int oldFocusedIndex = listView.SelectedItems[0].Index;
Later then you can restore focus executing:
listView.Items[oldFocusedIndex].Selected = true;
You can also make the item visible to the user due to the ListView's scroll bar will remain at the top after the Clear() method:
listView.Items[oldFocusedIndex].EnsureVisible();

Dropdown list selected item always set to default value

I wish to pass values of the selected items into a database and however noticed that the selected item is not what is sent into the database you can see in the snap shot below.
during run time the following value is recorded.
Where did it all go wrong with the dropdown lists selected item?
Counting on your intelligence...thanks.
Put your call to load the dropdownlist in
if (!Page.IsPostBack)
{
//LoadDropdownListHere();
}
Try DBNull.Value instead of null.
Edit:
I think you need to specify the type using this overload: http://msdn.microsoft.com/en-us/library/cc491445.aspx
CSMDataSource.InsertParameters.Add("CaseID", DBType.Int32, CaseIDDropDownList.SelectedItem.Text);
I found the problem for the dropdownlist always selecting the first value. It appears the dropdown list is always rebinded anytime i click a button that requires a post pack as such the data tha was binded at page load re binds a gain before the selected item gets picked as such the default first value gets picked all the time. to solve my problem i first disabled enable auto postback on the dropdownlist and in the code behind that is my .cs file during the page load, where i first binded the data to the dropdown list, i used a condition like if(!ispostback) to bind my data. what this does is when the page loads first time the dropdownlist is binded and if i should select an item from the drop down list, the "auto post back" that i disabled earlier on keeps the selected item selected and when i click and button that requires a post to the server, the (!ispostback) prevents the dropdownlist to be binded again before selection. so in effect, the selection is done first and afterwards if the page should load anew, the drop down list is binded again.
i was in a bit of a rush while typing so please bare with my typo errors. do call in anytime for more clarification...peace

selection issue in listview or TreeView

Hello i have a list view control,
While the form is being loaded i fill the list, i have aprrox. say 100+ items.
While filling i check some parameters and decide which item/row need to be selected.
i set the Selected property to true... refer the code below:
some lines here .....
ListViewItem listViewItem = new ListViewItem("COL1");
listViewItem.SubItems.Add("COL2");
check for some condition and then
listViewItem.Selected = true;
this.m_lstViewCtrl.Items.Add(listViewItem);
This does select the item, there are no issues with it...
however, say the ctrl is sized to see onlu say some 15 items, but the selection is say some 35th item.... currently the scroll bar appears the item is selected but i have to scroll to see what was selected?
is it possible to scroll to the selected item so that is selection is clearly visible...
Will the same apply for a Treeview?
Use the EnsureVisible property on the ListViewItem.
UPDATE: So, your code would be as follows:
listViewItem.Selected = true;
listViewItem.EnsureVisible();
this.m_lstViewCtrl.Items.Add(listViewItem);

Categories

Resources