Let's say I have a ListBox called animalList. As DataSource I use following class:
class Animal
{
private int id;
private string name;
private string description;
public Animal(int id, string name, string description)
{
// implementation
}
public int Id
{
// implementation
}
public string Name
{
// implementation
}
public string Description
{
// implementation
}
}
I'd like to have 2 columns in ListBox: Name and Description. Is it possible to do so?
I managed to add one column like this:
List<Animal> animals = // LINQ sucking data from XML
animalList.MultiColumn = true;
animalList.DataSource = animals;
animalList.DisplayMember = "Name";
animalList.ValueMember = "Id";
but I cannot really figure out how to actually implement next column.
The name of the MultiColumn property can be slightly misleading. The ListBox control does not support the type of columns you're looking for.
What MultiColumn actually does is "overflow" items into a new column instead of showing a vertical scrollbar.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.multicolumn.aspx
Related
I already have seen that there are many Topics about this,but I am not able to make them work.
I have an Combobox which shall be filled with an List of String.
this.elementBox.DataSource = this.elementList;
this.elementBox.SelectedIndexChanged += new EventHandler(this.elementBox_SelectedIndexChanged);
If I put this into the Code my GUI does not appear, I suppose the assignment of the data is wrong. I already tried Bindingsource.
Then I want to fill an Combobox with an List of Objects and this does not work either. itemData is an Dictionary with an String as key and the Objectlist as value.
box1.DataSource = itemData["ele1"];
box1.DisplayMember = "Name";
box2.DataSource = itemData["ele2"];
box2.DisplayMember = "Id";
I tried to play around with DisplayMember and Value but I am not able to get it to work.
The Type of the List is MyItem:
class myItem{ int Id; Internal data;}
class Internal {String name;}
Obviously I want to show the myItem.data.name in the Combobox, but I do not know how to get it. Furthermore what is the difference of DisplayMember and Value?
DisplayMember expects a public property not a private field
Change the classes:
class myItem
{
public int Id {get; set}
public Internal data {get;set;}
}
class Internal
{
public string Name {get; set;}
}
Note also that the property names are case sensitive. So 'name' and 'Name' are considered to be different entities.
Further, if you are adding an object of 'myItem` class, you cannot get the combo box to access the a member of a member. You will have to do the fetching yourself:
class myItem
{
public int Id {get; set}
public Internal data {get;set;}
public string Name { get { return data.Name; }}
}
Now by setting DisplayMember to "Name" it will reference the Name property of myItem which in turn accesses the data.Name property.
If the list comes to you from somewhere else (you don't own the list class and thus cannot modify it's code), you will have to wrap the objects inside another class and replace the list with a list of the wrapped objects:
class myItemWrapper
{
private myItem _myItem;
public myItemWrapper(myItem item)
{
_myItem = item;
}
public override string ToString()
{
return _myItem.data.Name;
}
public myItem Item { get { return _myItem;}}
}
then you can get rid of the DisplayMember assignment
box1.DisplayMember = "Name";
as the ToString() will be called instead.
but you have to replace the
box1.DataSource = itemData["ele1"];
with a bit more code:
var wrapper = new List<myItemWrapper>();
foreach(var item in itemData["ele1"]);
wrapper.Add(new myItemWrapper(item));
box1.DataSource = wrapper;
And don't forget that, when you are handling the selected item in the listbox that you are no longer dealing with myItem objects, but myItemWrapper objects instead. So to access the actual myItem object you will have to use the Item property of the myItemWrapper class instead.
I have a datagridview that displays data from a database table. There are two columns ID and NAME.
I have a textbox in which i enter name and data of those names appear in datagridview. I have achieved searching of data but i want to search as done in comboBox. When i type "a" then all names starting with "a" should appear in datagridview. Then if i type "arn" then all names starting with "arn" should appear in datagridview. I need to know if there is a built-in method or some LOGIC that i should consider.
I am using Linq to Sql.
EDIT-1
I have made a subclass in the form class
public class Table1
{
public int ID;
public string Name;
public MyList(string _newID, string _newName)
{
_id = _newID;
_name = _newName;
}
public int _id
{
get { return ID; }
set { ID = value; }
}
public string _name
{
get { return Name; }
set { Name = value; }
}
}
Used BindingList to bind it with dataGridView1. I have applied textChanged event on a textBox1.
BindingList<Table1> tempData = new BindingList<Table1>();
string name = textBox1.Text;
var result = from row in context.Tables
where row.Name == name
select row;
foreach (Table std in result)
{
tempData.Add(new MyList(row.ID, row.Name);
}
dataGridView1.DataSource = tempData;
This is how i am doing but this searches for exact same name. I want to make it like comboBox drop down search. In which at each key typed the search result gives names/strings containing those characters.
Thanks.
Try this
Entity Framework wildcards & Linq
Like operator or using wildcards in LINQ to Entities
http://www.codeproject.com/Articles/634104/Csharp-Wildcard-Search-Using-LINQ
Suppose I have a class name Employee. I have another class called Controller, which is a list of Employee objects. The point is that I cannot figure out how to make that list display in a listview.
Assume the following code (for the sake of simplicity, I will reduce the code to a bare minimum):
class Employee
{
string name;
string position;
public Employee(string inputName, string inputPosition)
{
this.name = inputName;
this.position = inputPosition;
}
public string getName() { return name; }
public string getPosition() { return position; }
}
Now the Controller class:
class Controller
{
List<Employee> employeeList;
public Controller()
{
employeeList = new List<Employee>();
}
public List<Employee> getEmployeeList() { return employeeList; }
}
Now, I want to display the NAMES in a listbox. So far I have:
Controller ctrl = new Controller();
ctrl.addEmployee("testname1", "testjob1");
ctrl.addEmployee("testname2", "testjob2");
listBox1.DataSource = ctrl.getEmployeeList();
This makes the listbox print out the current namespace of the list. Searching google led me to believe that I need to use listbox1.DisplayMember, but
listbox1.DisplayMember = "name";
does not help. It probably defaults to .ToString() because it doesn't find the 'name' property in the controller class. How could I handle this? I think I'm missing something obvious, I've been stalling on this for almost 2 hours, I'm not so experienced with .net.
The DisplayMember has to be a public Property and not a private field! (See this MSDN doc for DisplayMember information and a full example.)
You have:
string name; // by default this is a private field of the Employee class
You need:
public string Name { get { return name;} }
and:
listbox1.DisplayMember = "Name";
I have a list view with two columns- name and number. I want to read all these items and assign name to a combo box display member and number to value member. I have tried thinking the approach to follow but couldn't help myself. This is what I have tried. How should I proceed?
public class numbers
{
public string name;
public string number;
}
public class names : List<numbers>
{
}
names cname = new names();
public void addcontacts()
{
foreach(ListView lv in bufferedListView1)
{
//No idea how to proceed
First you set your own type:
public class myContact
{
public string Name { get; set; }
public string Number { get; set; }
public myContact(string name, string number)
{
this.Name = name;
this.Number = number;
}
public override string ToString()
{
return Name;
}
}
Then you transfer all items from the listview to the combobox like this:
foreach (ListViewItem item in listView1.Items)
{
comboBox1.Items.Add(new myContact(item.Text, item.SubItems[0].Text));
}
This example assumes, that each listviewitem holds the name and that its first child holds the number.
When you add objects to the combobox, C# will use the objects' ToString() method to create something that you can actually see when the program is running. You override the default ToString() method with your own and only return the name. If you want to use the selection from the combobox you just cast the selectedItem back to myContact and can access the number there. Welcome to OOP :)
The following two code snippets populate a BindingSource which is later assigned to
a DataGridView.DataSource.
When the concrete class QuotesTool.LineItem is used (first snippet) the grid DOES NOT display the appropriate data:
BindingSource lineList = new BindingSource();
foreach (XElement y in _lines.Elements())
{
lineList.Add(new QuotesTool.LineItem(
y.Element("Vendor").Value,
y.Element("Model").Value,
y.Element("Selling_Unit").Value,
y.Element("Net_Price").Value,
y.Element("Spec").Value
));
}
But, if an anonymous type is used the grid displays data OK:
foreach (XElement y in _lines.Elements())
{
lineList.Add(
new {
vendor = y.Element("Vendor").Value,
Model = y.Element("Model").Value,
UOM = y.Element("Selling_Unit").Value,
Price = y.Element("Net_Price").Value,
Description = y.Element("Spec").Value
});
}
Any ideas would be appreciated. Thanks.
Hard to tell without seeing QuotesTool.LineItem, but by default to be useful, each member:
must be public
must be a property (not a field)
must not be marked [Browsable(false)]
The issue here is usually one of the first two. For example, none of these will work by default:
public string Vendor;
internal string Vendor {get;set;}
[Browsable(false)] public string Vendor {get;set;}
but this will:
public string Vendor {get;set;}
Note that it doesn't have to be an automatically implemented property, nor does it need to be writeable:
private readonly string vendor;
public string Vendor { get { return vendor; } }