I use the following code to add a item to my listView...
ListViewItem^ subitem = gcnew ListViewItem();
subitem->SubItems->Add(textBox2->Text);
listView3->Items->Add(subitem);
What code would I add onto this so it can retrieve the index of this newly added item?
Thanks.
The index of the newly added item should basically be the last index of the collection. Try this :
int index = listView3->Items->Count - 1;
And also try this :
int index = listView3->Items->IndexOf(subitem);
Related
I am using an array to populate my listbox. It worked fine but when I set the listbox.selection.none (I don't want items to be selected) then My listbox adds item to its first index but after that. It keeps the same value in an does not add new values. below is the code what i am using to add items to listbox from array.
Private array(10) As Decimal ' adds student score to array
dim index as integer = 0 '
Private Sub PopulateScoreList() ' method called when button is clicked
If index < 10 Then
' adds value to array at every button click event
array(index) = Math.Round(value, 1)
End If
index += 1
lbxTroopersScore.DataSource = Nothing
lbxTroopersScore.DataSource = array
end sub
Seems like a bug. Toggling the SelectionMode property fixes the problem:
lbxTroopersScore.DataSource = Nothing
lbxTroopersScore.SelectionMode = SelectionMode.One
lbxTroopersScore.SelectionMode = SelectionMode.None
lbxTroopersScore.DataSource = array
I want selected column's index of the DataGrid. For example, if I select the first column, I want the first column's index (index = 0).
I tried it in the DataGrid SelectionChanged event, but I can't seem to get the particular column index. If any one knows how to do it, help me with some sample code.
The DataGrid.Items property returns a DataGridItemCollection representing the DataGridItems in the DataGrid.
Each DataGridItem is representative of a single row in the rendered table. Also, the DataGridItem exposes a Cells property which represents the no. of tablecells (in other words, the columns) in the rendered table.
// Get the Row Count
int rowCount = myGrid.Items.Count;
// Get the no. of columns in the first row.
int colCount = myGrid.Items[0].Cells.Count;
I am assuming that you want the indexes of any columns that are selected. Here is the code I've come up with:
List<int> selectedColumnIndexes = new List<int>(dataGrid.SelectedCells.Count);
for (int i = 0; i < dataGrid.SelectedCells.Count; i++)
{
foreach (DataGridColumn column in dataGrid.Columns)
{
if (column.DisplayIndex == dataGrid.SelectedCells[i].Column.DisplayIndex)
{
if (!selectedColumnIndexes.Contains(column.DisplayIndex))
{
selectedColumnIndexes.Add(column.DisplayIndex);
}
}
}
}
Thus you will have a list of all the indexes of the columns currently selected. This question gives some nice clues in what direction to head here.
Obviously, if you want just the number of columns actually selected, then that value is simply selectedColumnIndexes.Count after the for loops have run through.
I am using Synfusion's Data Bound Grid.
I have set two properties as
this.mygrid.TableModel.Options.EnterKeyBehavior = GridDirectionType.Right;
this.mygrid.TableModel.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
If no column is hidden this works fine. If i hide any column then it doesn't works.
Please help me to achieve enter key behaviour if columns are hidden.
Thanks in Advance
I have handled the following code for hiding columns :
GridColHidden[] hiddenCols = new GridColHidden[ 3];
for (int i = 0; i < 3; i++)
{
hiddenCols[i] = new GridColHidden(i + 1);
}
this.gridDataBoundGrid1.Model.ColHiddenEntries.AddRange(hiddenCols);
I have now added the following code.
this.mygrid.TableModel.Options.EnterKeyBehavior = GridDirectionType.Right;
this.mygrid.TableModel.Options.WrapCellBehavior = GridWrapCellBehavior.WrapGrid;
I was able to see the first cell is set as current cell , when Enter key is pressed with the current cell's position placed in last cell in Grid.
I am trying to find the index of a particular value from the CheckedListBoxControl. The CheckedListBoxControl has a DataSource, DisplayMember, ValueMember set to a DataTable and two columns receptively. Now I have to set the CheckedState Property to true by finding its index from CheckedListBoxControl by using some value from the ValueMember and then calling the SetItemChecked() method with that index.
I'm not able to find any property or method which returns the index. Please help.
If a list box control is bound to a data source, you can iterate throught all listbox items using the the GetItem() method and the ItemCount property:
for(int i = 0; i < checkedListBoxControl.ItemCount; i++) {
object dataRow = checkedListBoxControl.GetItem(i);
}
To find the index of the specified item you can use the FindItem() method
searching by DisplayText:
string s = "searchString";
int index = checkedListBoxControl.FindItem(startIndex, true, delegate(ListBoxFindItemArgs e) {
e.IsFound = s.Equals(e.DisplayText);
});
searching by ValueMember:
object value = 100;
int index = checkedListBoxControl.FindItem(startIndex, true, delegate(ListBoxFindItemArgs e) {
e.IsFound = object.Equals(value, e.ItemValue);
});
Please also take a look at the "How to get checked rows of a data-bound CheckedListBoxControl" article.
This article shows how to implement a copy operation on a drop event. I'd like to do the same but I want my dropped item to appear in the collection according to where it was placed on the UI. So I need the StartIndex much like on a NotifyCollectionChangedEventArgs when an ObservableCollection changes. In the article you'll see that eventually you get a SelectionCollection object whose items have an Index property. But unfortunately this is the index of the source collection (where it was picked) and not the destination collection (where it was dropped).
Ok, this is quite ugly, but I didn't find another way, not by myself and also not by searching the net for answers. Must have been another deadline at Microsoft that prevented the rather obvious functionality to be included...
Basically the method below does everything manually, getting the drop location and checking it for listbox items to use as index references.
private void ListBoxDragDropTarget_Drop(object sender, Microsoft.Windows.DragEventArgs e)
{
// only valid for copying
if (e.Effects.HasFlag(DragDropEffects.Copy))
{
SelectionCollection selections = ((ItemDragEventArgs)e.Data.GetData("System.Windows.Controls.ItemDragEventArgs")).Data as SelectionCollection;
int? index = null;
if (selections != null)
{
Point p1 = e.GetPosition(this.LayoutRoot); // get drop position relative to layout root
var elements = VisualTreeHelper.FindElementsInHostCoordinates(p1, this.LayoutRoot); // get ui elements at drop location
foreach (var dataItem in this.lbxConfiguration.Items) // iteration over data items
{
// get listbox item from data item
ListBoxItem lbxItem = this.lbxConfiguration.ItemContainerGenerator.ContainerFromItem(dataItem) as ListBoxItem;
// find listbox item that contains drop location
if (elements.Contains(lbxItem))
{
Point p2 = e.GetPosition(lbxItem); // get drop position relative to listbox item
index = this.lbxConfiguration.Items.IndexOf(dataItem); // new item will be inserted immediately before listbox item
if (p2.Y > lbxItem.ActualHeight / 2)
index += 1; // new item will be inserted after listbox item (drop location was in bottom half of listbox item)
break;
}
}
if (index != null)
{
foreach (var selection in selections)
{
// adding a new item to the listbox - adjust this to your model
(lbxConfiguration.ItemsSource as IList<ViewItem>).Insert((int)index, (selection.Item as ViewItem).Clone());
}
}
}
}
}