I'm using ObjectListView with C# and .Net 4.0. I wrote code that reloads the listview and then re-selects the last selected index.
The re-selection code is quite simple:
olvListView.SelectedIndex = i;
This appears to work, because the item is selected. However, if I then click the up or down arrow, the selection jumps up to the second row (no matter what row I selected), suggesting that the selection was actually set on the first row, no matter what was the value of i.
What am I doing wrong here?
The underlying ListView Control distinguishes between 'selection' and 'focus'.
olvListView.SelectedIndex = i; changes the selection but not the focus. But the focused row is the one that the keyboard input relates to.
Either change the focus the as well
olvListView.SelectedIndex = i;
olvListView.FocusedItem = olvListView.SelectedItems[0];
or call
olvListView.SelectObject(aModelObject);
The second solution would be the preferred way to select an item when working with OLV, however you say you "wrote code that reloads the listview", so the reference to the original item is probably different. Maybe you should just refresh the items that changed, instead of reloading everything. That way you could preserve the selection.
Example if your olv have datasource from "class_z.list" and foreach have only one result.
foreach(class_z a in class_z.list.Where(x=>x.id==id_value))
{
olv.SelectedObject = z;
}
Related
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.
I have a data loaded from database into dataGridView, with effect looking like this:
(in final version I would prefer to hide the PatientID column)
What I'm trying to do, is return value of PatientID when user clicks ANYWHERE in the corresponding row (i.e. user clicks "Doe" and value returned is "2"). Could anyone give me a hint how to do this? I don't think there is valueMember property... I was trying Rowindex but that returns value of number of row counting from the top(D'uh?!)
Also, is there a way for user to highlight whole row when clicking on the single cell?
EDIT: Oh God, I've spent few hours late at night to find this, In the morning I gave up and posted here... just to find answer 5 minutes later:
string test = dataGridView1.Rows[e.RowIndex].Cells["PatientID"].FormattedValue.ToString();
Still, that leaves my second question about highlighting whole row.
If the datagridview is bound to some data source (DataView), you can use DataBoundItem property, for example
DGV.CurrentRow.DataBoundItem["PatientID"]
or
DGV.SelectedRows[0].DataBoundItem["PatientID"]
or
DGVUnderlyingBindingSource.Current["PatientID"]
If the DataGridView is bound to a strongly typed data source (e.g. BindingList) then you can use the above like this:
((PatientType)DGV.CurrentRow.DataBoundItem).PatientID
or
((PatientType)DGV.SelectedRows[0].DataBoundItem).PatientID
or
((PatientType)PatientTypeBindingSource.Current).PatientID
About the second part of the question, set the DataGridView's SelectionMode property to FullRowSelect
EDIT
You can't use the solution from your edit if you hide that column. In order to access the value by using .Cells[idx].FormattedValue that value must be visible. But you can use this one even if you hide the column.
For the second question, set selection mode to FullRowSelect and the complete row will be highlighted when the user clicks on it. You can set this attribute in the designer or code:
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
I don't know if the title express what I want. I have a ListBox in WPF where I generate many elements. When I click on a element while still generating I want my selected item to not move down the list, so I cannot see it anymore, I want to stay in the exact position where I click on it.
If this is possible, can someone point some ideas on how to do it in C#?
Thanks.
Assuming that this is even a good idea and that you are using winforms
Step 1:
Determine the index of the selected item in the source.
Step 2:
When your adding items to the ListBox split the ListBox at the index where the item previously was insert the item at that point, then add on the remainder of the items, while making sure that you've removed the item if it is now elsewhere in the list.
Code:
//Let's assume that you know how to get the position of the item when it is clicked and save the
//item to a variable called OriginalItem
public void PutTheItemInTheSameSpot()
{
var listboxitems = (List<Integer>)YourListBox.DataSource;
var originalClikedItem = OriginalItem;
var topPart = new List<Integer>();
for (i = 0; i < itemPosition; i++)
{
topPart.Add(listboxItems[i]);
}
topPart.Add(originalClickedItem);
var bottomPart = listboxitems.Remove(toppart);
YourListBox.DataSource = toppart.AddRange(bottomPart);
}
Saw your edit about it being WPF
The could should work in idea.
Just a thought: you could try having your view respond to an event whenever an item is added to your ListBox. In the event handler, you could force the selected item to scroll into view presumably keeping it in the current "viewable" position:
listBox.ScrollIntoView(listBox.SelectedItem);
I've never tried this before so it may or may not produce the desired affect?
I cannot able to set focus on User Control Item
I am using DataGrid in User Control
I want to set focus on First row of DataGrid
I found following code
int index = 0;
dgAccountInfo.SelectedItem = winAccountInfoGrid.dgAccountInfo.Items[index];
dgAccountInfo.ScrollIntoView(winAccountInfoGrid.dgAccountInfo.Items[index]);
DataGridRow dgrow = (DataGridRow)dgAccountInfo.ItemContainerGenerator.ContainerFromIndex(index);
if (dgrow != null)
{
dgAccountInfo.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next))
};
By running above code I found ContainerFromIndex method always returns null
To rid of null problem I found following link
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ab95dd62-995f-481a-a765-d5efff1d559c/
I come out of null problem using above link but still focus is not set on data grid
Thanks for reading my question
It appears that, in the code you're using, you are data binding to something else, which is unnecessary if you are only wanting to set the focus to the first item. If your only intention is to set the focus (selected index) of the datagrid, that can be accomplished in one single block of code.
if (dgAccountInfo.HasItems){
dgAccountInfo.SelectedIndex = 0;
}
The if construct ensures that your datagrid actually has items in it (otherwise, the next line of code would throw an exception). If the datagrid has items, it executes the code inside the construct. If the datagrid has no items, it skips right over the code.
Even if you never plan to have no items in your datagrid, you should include this if construct anyway, in the off chance that something does happen
Inside the if construct, you set the datagrid's SelectedIndex to the first item. Remember that, like with a list box, a datagrid is "zero-based," meaning that the first item has an index of zero. To select nothing in the datagrid, set the selected index to "-1"
In the end, setting "SelectedIndex" is far more reliable than setting "SelectedItem," as the items in a datagrid usually change.
If you wanted, you could include an #else statement in the above code (just above endif) if you wanted to do something else if the datagrid has no items.
I hope this helps!
Okay big brains here's something that's more of a challenge than a requirement. I am a bit stumped. I usually just need a prod in the right direction, so get your prodding sticks ready.
I have a tabcontrol covered in textboxes. I want to perform a check of the contents of all the textboxes during the SelectedIndexChanged event on a listview on the same form. If one of the textboxes has data different from a DataTable row - represented by the ListView Item - I want it to ask if the user would like to keep the change they just made. If nothing has changed I want it to just change the selection.
So obviously I'm comparing the contents of the text boxes against associated columns in the datarow.
I could just brute force the check and do each individual check one at a time. I'd prefer to come up with some clever algorithmic way of cycling through the tabcontrol textboxes and checking the values against the columnar values.
Any suggestions?
EDIT: I like the "cleverly named textboxes" solution below best, although both are good. If no one else has a better idea in the next 14 days the textbox answer gets the green.
Give the textboxes a clever name as in a portion of the name is the column/row name.
Group the textbox controls an loop through them. For each control, get the (portion)name and use that as a reference to your datatable. Check the values.
If I'm understanding you right, you want to avoid comparing every textbox on every change, in favour of just checking the textboxes that are changed, driven by the SelectedIndexChanged event of the ListView control. Is that right?
Well, DataRows and DataTables already have row versioning and rollbacks implemented, so if you bind the text boxes to the underlying row (either by writing events to write back on change/lose focus or by using an automated mechanism to accomplish the same task), then check the RowState property on SelectedIndexChanged. If the RowState is anything other than unchanged, prompt the user to save. If he saves, commit the changes, otherwise reject them.
So, for example, you'd want something like this in your SelectedIndexChanged event handler:
if (row.RowState == DataRowState.Modified) {
// prompt for user input
if (promptResult == PromptResult.Save) {
row.AcceptChanges();
}
else {
row.RejectChanges();
}
}