Trigger Event when listView Item is selected programmatically - c#

I wrote an application with listViews which fill other listViews on click event. Means that when the Row in the first listView is selected it fills the second listView with the related list like dynamic dropdown lists. I am googling for hours now but I cannot find a solution.
This is what I tried but nothing will trigger the listView1_SelectedIndexChanged Event.
Thanks for your help it is very appreciated.
I tried this after researching:
listView1.Focus();
listView1.Items[index is variable].Selected = true;
listView1.Click += new EventHandler(listView1_SelectedIndexChanged);
It actually selects the row in the listView and also the row gets highlighted blue but the event is still not triggered.

Related

Listbox selectedIndexChanged doesn't fire when selecting an item from the list

I have a listbox that needs to be populated by directories.
i have successfully filled the listbox with the Directories but my problem is i can't get the selected item's value. i placed a breakpoint on the SelectedIndexChanged event but my web application doesn't seem to run through that code.
Here is my code for populating the listbox:
lstDirectories.DataSource = dtDirectories;
lstDirectories.DataValueField = "DirectoryID";
lstDirectories.DataTextField = "DirectoryName";
lstDirectories.DataBind();
This is how i get value from the listbox on the SelectedIndexChanged Event:
TextBox1.Text = lstDirectories.SelectedItem.Value.ToString();
am i doing something wrong here? thanks for the help! :)
Use the property:-
AutoPostBack="True"
By default it is False.

DataGridView Cascading ComboBox displays black color backcolor in 2nd combobox

I have winform application with DataGridView and there I am using Cascading ComboBox. For example column 1 has one ComboBox and value of column 2 combobox depends on column 1 selection. Everything seems to be fine but when i open 2nd combobox then its backcolor gets black. See the screenshot. I am not setting backcolor anywhere (in design or code). Any idea why this strange behavior?
I am binding 2nd ComboBox in EditingControlShowing event of DataGridView. If i bind that in FromLoad event then it works fine.
Check the DefaultCellStyle of the column. It's the only place I see where I can duplicate the blackness.
In combobox DropDown event I am setting BackColor = Color.White;. So its a workaround I found for now. Any other solution is welcomed.
it's an old post but someone might need this like me. Why not try:
cmbBox.SelectionChangeCommitted += new EventHandler(comboBox_SelectionChangeCommitted);
instead of:
cmbBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);

DataGridView first column,first row, is selected on Load, I don't want this

So basically the very first column in the first row is always selected, I can't figure out a way to have it so the gridview has no selected cells. Any help?
I was having quite a bit of trouble with this myself. I have a user control with a DataGridView that is populated on application/form load. The selection of the first row seems to happen after databinding is complete and the grid is populated/rendered. The user control load event (and presumably, form load as well) fires prior to that - so calling gridView.ClearSelection() or nullifying gridView.CurrentCell in those load events has no net effect.
What finally worked for me was calling .ClearSelection() from the DataBindingComplete event of the DataGridView itself. This worked like a charm.
I had this same issue and nothing was working. The solution that worked for me was setting the 'Tabstop' property to False and calling the ClearSelection() method immediately after the data bind.
Set the DGV's CurrentCell property to null after data binding the DGV:
dataGridView1.CurrentCell = null;
Note that doing this won't prevent DGV events associated with row and cell selection from firing; you'll have to add selected row or cell count checks on RowEnter events, something like this:
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.SelectedRows.Count == 1) {
// Do stuff since a row is actually selected ...
}
}
after bounding data just call
dataGridView.ClearSelection();
I think you tried to call it before setting data to dataGrindView, if you even ever tried it
You should call: ClearSelection after event: DataBindingComplete
I had the same issue in my case, instead of set the first row visibility to false. It would be better to set the GridColor value to avoid risk on SelectionChanged Event.
Put dgv.ClearSelection() on DataBindingComplete Event and set GridColor to your DataGridView BackColor.
Set GridColor to visible color (e.g. : Gray) on your populate method / firing event.
IF I understand the Q. This prevents a cell from showing selected after data binding.
So the back color stays white. You can also Edit the columns and set it there.
DataGridView.DefaultCellStyle.SelectionBackColor = DataGridView.DefaultCellStyle.BackColor;
I also wanted read-only DataGridView, and in my case, a separate thread is slowly obtaining data and handing it to the GUI thread via a multi-thread list, and Form timer. In this approach, the GUI thread expands the data grid as needed while allowing browse.
With suggestions above the selection could be hidden, but none could prevent the cell from getting reset when my GUI thread calls dataGridView.Rows.Add() with a selection. This includes hooking events to prevent the selection, and disabling edit mode.
I found the behavior I wanted with
dataGridView.AllowUserToAddRows = false;
Now I have a dynamically sized, asynchronously loaded data grid that is read-only.
I did not like the BackgroundWorker solution, because progress is quite a burden on my loading code. Nor did I like the requirement to rebuild a new DataTable every refresh of the grid. I could not find any hints on refreshing the DataGridView with one DataTable that is being built up, but it seems like this should be possible.
Make sure your are not calling the method to load the data from the form constructor. If you call it from the form.load()
also after the datagridview is loaded do this
DataGridView.Rows[0].Selected = false;
Most of the time, it is caused by a small mistake, maybe the datagridview is set on a group box. If there are more group boxes then the selection will stop on the first group box, so keep the group box by priority basis.
I had the same problem and have solved it by overriding the OnPropertyChanged event of the GridView
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
this.ClearSelection();
}
Event _MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
GridView gv = (sender as GridView).GetDetailView(e.RowHandle, e.RelationIndex) as
GridView;
gv.ClearSelection();

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);

ComboBox SelectionChanged event firing off unexpectedly

I have 4 ComboBoxes arranged horizontally :
The titles are just for reference.
[Platforms] [Releases] [Release Items] [Release Item Products]
My UI model for the boxes is as follows:
At first all four ComboBoxes are disabled (IsEnabled = false)
When the page loads, an async callback gets the Platforms data and binds it to the Platforms comboBox after Enabling it, and manually add "All" as an item.
User selects a platform
If the user selects "All" , I do the following: Disable the other three comboboxes
and call the Async method to rebind data to the platforms combo box.
releasesFilterComboBox.IsEnabled = false;
releaseItemsFilterComboBox.IsEnabled = false;
releaseItemProductsFilterComboBox.IsEnabled = false;
mainClient.GetBuildsAsync(null);
If the user selects some other platform (other than 'All'), based on which platform is selected, I collect all the items that match the platform from the object in the DataContext, and set the ItemsSource of the Releases comboBox to it as follows:
BuildsListBox.ItemsSource = platformSelectedBI = from bi in buildInfos where
bi.GetBuildsResult.ReleasePlatform == selectedPlatform
select bi;
releasesFilterComboBox.IsEnabled = true;
List<String> releaseNumbers = ( from bld in buildInfos.Where(bi => bi.GetBuildsResult.ReleasePlatform == selectedPlatform)
select bld.GetBuildsResult.ReleaseNumber ).ToList();
releaseNumbers.Insert(0, "All");
releasesFilterComboBox.ItemsSource = releaseNumbers.Distinct();
The rest of the ComboBoxes follow the exact same procedure to bind data, and trigger
and actions are performed in a method which is called when the SelectionChanged events are fired.
etc etc -> same for the rest of the boxes.
My Problem is: After I've populated at least the first two ComboBoxes, when I select
the 'All' option on a PREVIOUS ComboBox, after the expected behavior (other comboboxes are disabled), when I select an option other than 'ALL', the SelectionChanged event is triggered for EVERY COMBOBOX that was filled previously! (all that were disabled). ###
Just to clarify, if it isn't yet.
I've selected a Platform, and so Releases is populated. I've then selected a Release, and so Release Items is populated. Now, when I select 'ALL' on the Platforms combobox, the Release and Release Items boxes are disabled. Then, when I select a platform other than 'ALL', the SelectionChanged event is fired, not just for Platforms, but also for Releases and ReleaseItems.
The ONLY change I'm making to the comboboxes in the second part of the event handler is SETTING THE ITEMS SOURCE like the in code shown above. Nothing more.
Why are the SelectionChanged events being fired for the other comboBoxes?
Is this a known bug in SL comboboxes? Can I prevent this from happening?
I'm sorry if the question and explanation isn't very clear. I'll try my best to clarify if you ask me.
Thanks a lot!
It all comes down to re-population of the lists. One way you could fix it would be to remove the event handlers when you select "ALL" and disable the other combo boxes. Then if you select something other than "ALL" you could set the datasources and then re-add the selection change event handler.
Another way would be to leave them all there and then in the "ALL" selection set the selected index to -1 (default) and in your event handler don't do anything if it's -1.
Just a thought.

Categories

Resources