Get ListView Item Values of row tapped. I am able to get the item tapped in code behind like so:
private async void Item_Tapped(object sender, ItemTappedEventArgs e)
{
ListView listView = (ListView)sender;
if (listView != null)
{
string pName = e.Item //.PNname; **<<-- This is returning my bound values (4 items) as PName, PNumber... and so on**
}
}
I try to type the .PName it is a sub value to the e.Item list but that is invalid. I really need to grab by name because it appears the return selected row item returns values in a random way? One time PName will be first then next it might be 2nd or 3rd?
Anyway what am I missing to grab the values I need?
I'm doing a lot more visual things here but wanted to keep this code very simple to what I am actually having an issue with getting the individual values in the row.
TIA!
Cheers!
Rick...
e.Item is an object (datatype), if I am not mistaken. You have to cast e.Item to the right data type first:
string pName = (e.Item as Person)?.PName;
(assuming that PName is a property of the class Person)
Try get the itemIndex clicked and get the element in the list or use the AutomationId.
Related
I am working with C# .NET 4.0
I am trying to get the value of a single selected item in a listbox.
This is how I populate the control:
this.files_lb.DataSource = DataTable object
In my designer, I have specified file_name as the DisplayMember and file_id as the DisplayValue
After selecting an item in the listbox, I tried the following to get the value:
this.files_lb.SelectedValue.ToString()
But all it returns is "System.Data.DataRowView".
At this link : Getting value of selected item in list box as string
someone suggested -
String SelectedItem = listBox1.SelectedItem.Value
However, 'Value' is not an option when I try this.
How can I get the ValueMember value from a single selected item in a listbox?
var text = (listBox1.SelectedItem as DataRowView)["columnName"].ToString();
Replace columnName with the name of the column you want to get data from, which will correspond with a column in your datasource.
Also watch out for nulls if there's no selected item.
It really should be this easy; I have the following in a click event for button to make sure I wasn't over simplifying it in my head:
private void button1_Click(object sender, EventArgs e)
{
string selected = listBox1.GetItemText(listBox1.SelectedValue);
MessageBox.Show(selected);
}
And the result:
Edit
It looks like your issue may be from not setting a property on the control:
Select the ListBox control
Click the little arrow to show the binding / items options
Select Use Data Bound Items
If I deselect that box, I get the exact same behavior you are describing.
var selectedValue = listBoxTopics.SelectedItem;
if (selectedValue != null)
{
MessageBox.Show(listBoxTopics.SelectedValue.ToString());
}
You may need to set the DataValueField of the listbox.
NewEmployeesLB.DataSource = newEmployeesPersons.DataList.Select(np => new
ListItem() { Text = np.LastName + ", " + np.FirstName, Value = np.PersonID.ToString() }).ToList();
NewEmployeesLB.DataTextField = "Text";
NewEmployeesLB.DataValueField = "Value";
NewEmployeesLB.DataBind();
In my DataGrid I used a ComboBox in the first column, so it will fetch some data from the database using the DisplayMember and ValueMember concepts. Now I want to remove the value from the ComboBox which is previously selected.
Populating the ComboBox code is given below:
dTable = getDummyTable.GetDummyTble("Dummy", "DNO", "All");
dCmbData = dTable;
cmbDeno.DataSource = dTable;
cmbDeno.DisplayMember = "dyName";
cmbDeno.ValueMember = "dyRemarks";
The next row of selection there should not be a duplicate value in the ComboBox.
How can I achieve this?
Can anyone help me with this?
well try this one out. You have the DataTable which you are using to bind all combos (correct me if I'm wrong). Now all we need to do is that when an item is selected from any combo, we need to remove that item from all the other combos). You will need to declare a class level dictionary to store which combo had what value stored previously:
IDictionary<ComboBox, DataRow> _prevSelection;
//Please don't mind if syntax is wrong worked too long in web
comboBox.OnSelectedIndexChanged += fixItems;
private void fixItems(object sender, EventArgs e)
{
var cbo= sender as ComboBox;
if(cbo==null) return;
var prev = _prevSelection[cbo];
var row=<GET ROW FROM DATATABLE FOR CURRENT SELECTED VALUE>;
_prevSelection[cbo] = row;
UpdateOtherCombos(cbo, prev, cbo.SelectedItem.Value);
}
private void UpdateOtherCombos(ComboBox cbo, DataRow prev, object toRemove)
{
foreach(var gridrow in <YourGrid>.Rows)
{
var c = <FIND COMBO IN ROW>;
if(cbo.Id == c.Id) continue;//combo that triggered this all
var itemToRemove=null;
foreach(var item in c.Items)
{
if(item.Value == toRemove)
{
itemToRemove = item;
break;
}
}
//or you can get index of item and remove using index
c.Items.Remove(itemToRemove);
//Now add the item that was previously selected in this combo (that
//triggered this all)
c.Items.Add(new ComboBoxItem{Value = prev["ValueColumn"],
Text = prev ["TextColumn"]});
}
}
This is just to give you an idea that may help you to find an optimal solution rather than iterating over all combos as it will slow down if you have too many rows in grid or too many items in combos. Still even with this you need to put up some functions/code to make it working. Also please note that I have not worked on WinForms for some time and you need to check if things like ComboBoxItem etc. and any functions called on them exist. :)
I have a list box that is pulling it's data from a sql table. I have its selection mode set to multisimple.
I have the display member for each item set to the Name field from the sql table and the value member for each item set to the ID field from the sql table.
When I select multiple items from the list, how do I get the multiple values? I am able to get the text of one item by using the listboxname.Text but I don't know how to get each value that has been selected all at once.
Any help will be greatly appreciated.
Use the SelectedItems property, the following will get a List<string> representing the selected items:
var items = listBox1.SelectedItems.Cast<object>()
.Select(x=>Convert.ToString(x)).ToList();
To get the exact values (ID), it depends on the underlying item type. If you use a DataTable as DataSource for your listBox, the underlying item type is DataRowView, so the code to get the values IDs should be:
var ids = listBox1.SelectedItems.Cast<DataRowView>()
.Select(row=>row.Row.Field<string>("ID")).ToList();
//suppose the ID field is string
If the underlying item type is just a normal class with 2 properties Name and ID, such as call it Item, the code is simpler like this:
var ids = listBox1.SelectedItems.Cast<Item>()
.Select(item=>item.ID).ToList();
Method 1 : You can use SelectedIndices property of the ListBox to get the All selected items Index values.
then for each selected index you can get the Value of the Item.
Try This:
private void button1_Click(object sender, EventArgs e)
{
List<String> SelectedItems=new List<String>();
foreach (int i in listBox1.SelectedIndices)
{
SelectedItems.Add(listBox1.Items[i].ToString());
}
}
Note : List SelectedItems contains all SelectedItems from the ListBox
Method 2:
You can use SelectedItems property of the ListBox to get the All selected item values directly.
Try This:
private void button1_Click(object sender, EventArgs e)
{
List<String> SelectedItems=new List<String>();
foreach (String s in listBox1.SelectedItems)
{
SelectedItems.Add(s);
}
}
Note : List SelectedItems contains all SelectedItems from the ListBox
I try to get a category-id from the selected item in the dropdownlist. I fill the dropdownlist with a foreach loop (using a list).
This is the code that I am using to get the ID:
protected void dropDownCategories_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Category category = (Category)ddl.Items[ddl.SelectedIndex];
int CatID = category.CategoryID;
}
The code above doesn't work, and gives me this error:
Error 2 Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'XXXXXX.classes.Category'
But when I use the similar code for a listbox, the code below works!
protected void listBoxCategories_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender;
Category category = (Category)lb.Items[lb.SelectedIndex];
int CatID = category.CategoryID;
}
Why does this piece of code work for a listbox and not for a dropdownlist?
Thanks!
System.Web.UI.WebControls.DropDownList has a ListItemCollection as its Items property, which is a list of ListItem, an object that has a text and a value properties.
while
System.Windows.Forms.ListBox has an ObjectCollection as its Items Property, which is basically a list of objects
so when you add an item to a drop down list you are adding a List Item, which can't be converted to your type, i will need to see your drop down list filling code to know what you are storing in each item.
however when you add an item to a list box, you are adding a object, so if in each item you add an instance of your class then it can be converted back by casting.
I am listing set of table items(only names) in List View. I enabled checkbox property of ListView. I displayed all items as Large icons. I want to attach a key(id) for that items for further processing on that items.. If any idea , please reply
Use the ListViewItem.Name property. Poorly named, it is actually the key. The one you can pass to ListView.Items.IndexOfKey() or ListView.Items["key"].
Description
You should use the Tag property for things like that.
Gets or sets the object that contains data about the control.
You can set your id or any other object to the ListItem you want.
Sample
Add the Item
ListViewItem myItem = new ListViewItem();
myItem.Tag = 1; // or any other object
listView1.Items.Add(myItem);
Use the index
private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
ListViewItem myItem = e.Item;
int index = (int) myItem.Tag; // cast to your object type, int in this case
// use the index
}
More Information
MSDN - Control.Tag Property