I set Datasource of toolkit:AutoCompleteBox to a list of string items. it work good but when I want add a new item to list then set again the list to Datasource of toolkit:AutoCompleteBox , it is not added.
I think Datasource of toolkit:AutoCompleteBox is not updated.
have you any idea on how to add a new item ? or refresh or update list?
List<string> c1 = new List<string>();
//... add some items to c1
txt11.ItemsSource = c1;
c1.Add(txt11.Text.Trim());
txt11.ItemsSource = c1;
Oh my GOD, i found problem easy with this code:
i have to assignment again. first time give null to AutoCompleteBox.
List<string> c1 = new List<string>();
//... add some items to c1
txt11.ItemsSource = c1;
c1.Add(txt11.Text.Trim());
txt11.ItemsSource = null;
txt11.ItemsSource = c1;
Related
Is it possible to bind a ListBox to a DataSource of List<A>, while setting the DisplayMember to a custom string composed of A's properties, such as the following $"{A.b} {A.c}"?
var list1 = new List<A>();
// populate list
MyListBox.DisplayMember = $"{A.b} {A.c}"; // not going to work
MyListBox.DataSource = list1;
I know I can use an anonymous type, but I want the ListBox.Items to remain of type A. I don't want to do the following,
var list2 = (from a in list1
select new {A = a, DisplayMember = $"{a.b} {a.c}"}).ToList();
MyListBox.DisplayMember = "DisplayMember";
MyListBox.DataSource = list2;
because now MyListBox.Items is anonymous and A can't be retrieved like var list = MyListBox.Items.OfType<A>().
So, is there any way to do that?
I was thinking about it wrong. Solved it now. Maybe it was obvious to some, but I'm going to post my answer for posterity.
Use the anonymous type in the second example, and just set the ValueMember to A
var list2 = (from a in list1
select new {A = a, DisplayMember = $"{a.b} {a.c}"}).ToList();
MyListBox.DisplayMember = "DisplayMember";
MyListBox.ValueMember = "A";
MyListBox.DataSource = list2;
var list3 = MyListBox.Items.OfType<A>().ToList(); // works!
I have a combobox with the following structure. Also, I am getting a fld_id from another source and based on that id I need to select the corresponding item in the ComboBox. How can I do that?
comboBoxCustomers.DataSource = customers;
comboBoxCustomers.ValueMember = "fld_id";
comboBoxCustomers.DisplayMember = "fld_name";
Example:
List could contain these items
fld_id fld_name
65 Item1
68 Item2
69 Item3
I need to set Item 68 as selected.
Use Following:
comboBoxCustomers.SelectedValue = fld_id(which you are getitng from another source)
I don't have enough reputation to post a comment. This:
comboBoxCustomers.SelectedValue = fld_id
works great :) But AFTER showing the form, otherwise it will fail.
if you use datasource of the combobox, you can cast the datasource back to the list, find the item and use that item to set the selected item:
var stores = cbxStores.DataSource as List<store>;
var store = stores.Where(w => w.store_code == _station.store_code).FirstOrDefault();
cbxStores.SelectedItem = store;
Simplest procdure I found for myself:
You can bind it into some function with perameters while funtion is called.
Hope it is going to help you guys:
int Idd = Convert.ToInt32(your value for the combobox you want to be selected);
for (int i = 0; i < myComboBox.Items.Count; i++)
{
myComboBox.SelectedIndex = i;
if (Convert.ToInt32( myComboBox.SelectedValue ) == Idd )
{break;}
}
I got 3 List (L1,L2,L3) and i want to show them on ListView.But i cant add or convert, List data's to ListViewItem.Something like this
List<string> for96 = new List<string>();
List<string> for97 = new List<string>();
List<string> for98 = new List<string>();
ListView lv1=new ListView();
ListViewItem lvitem = new ListViewItem();
lvitem.Text=for96;
lvitem.items.add(97);
lvitem.items.add(98);
lv1.items.add(lvitem);
You could take a variety of routes with this, but here's one possibility.
Join the three lists of strings together and then set that as the data source for your ListView:
var combinedLists = for96.Union(for97).Union(for98).ToList();
listView.ItemsSource = combinedLists;
i have two different Lists.
List<MyClass> list1 = new List<MyClass>();
List<OtherClass> list2 = new List<OtherClass>();
I bind one of them to a xtragrid form devexpress:
gridControl.DataSource = list1;
That works. Now i want to change the list but the columns doesn't change.
Can someone help me?
Call gridView.PopulateColumns().
I've a list
List<String> SampleList=new List<String>();
I need to fill a listView with the contents of the list
For example the "SampleList" contains
a
b
c
d
The listView should be filled like
S.No Item
1 a
2 b
3 c
4 d
Now i'm using for loop for this method
like
for(int i=0;i<SampleList.Count;i++)
{
listView1.Items.Add((i+1).ToString());
listView1.Items[i].SubItems.Add(SampleList[i]);
}
is there any other way to do this like data binding ?
Thanks in advance
Not quite like databinding, but you could use VirtualMode and RetrieveVirtualItem
listView1.VirtualMode = true;
listView1.RetreiveVirtualItem += new RetrieveVirtualItemEventHandler( this.RetrieveVirtualItem );
listView1.VirtualListSize = SampleList.Count;
private void RetreiveVirtualItem( object sender, RetrieveVirtualItemEventArgs e )
{
ListViewItem lvItem = new ListViewItem((e.ItemIndex + 1).ToString());
lvItem.SubItems.Add(SampleList[e.ItemIndex]);
e.Item = lvItem;
}
Does it have to be a ListView? ListBox is simple:
using (Form form = new Form())
{
List<string> strings = new List<string> {"abc", "def", "ghi"};
form.Controls.Add(new ListBox() {DataSource = strings});
Application.Run(form);
}
For a richer display, DataGridView would also do this, but you need an extra bit of indirection (since it supports multiple columns, it needs a wrapper object per row):
using (Form form = new Form())
{
List<string> strings = new List<string> {"abc", "def", "ghi"};
var indirect = (from s in strings
select new {Text = s}).ToList();
form.Controls.Add(new DataGridView() { DataSource = indirect });
Application.Run(form);
}
This also gives you opportunity to add in extra data, for example the number:
var indirect = strings.Select((s,i) =>
new {Index = i + 1, Text = s}).ToList();
Unfortunately windows forms ListView doesn't support DataBinding. But if you update the list frequently, maybe you can use INotifyProperty interface.