C# combobox ValueMember - c#

I am experiencing some problems while setting ValueMember property of my ComboBox.
The line comboBox1.ValueMember = "Code"; breaks my code.
Here is my code:
Form1.cs:
private void Form1_Load(object sender, EventArgs e) {
...
...
MAPList MAP = new MAPList();
comboBox1.DataSource = MAP.All;
comboBox1.ValueMember = "Code";
...
...
}
MAPList.cs:
public class MAPList {
public readonly List<MAP> All;
public MAPList() {
All = new List<MAP>();
var MapData = // Getting map data
foreach(MAP m in MapData) {
All.Add(new Map(m.Name, m.Code));
}
}
}
MAP.cs:
public class MAP {
public readonly string Name;
public readonly string Code;
public RadioCode(string name, string code) {
Name = name;
Code = code;
}
public override string ToString() {
return String.Format("{0}: {1}", Name, Code);
}
}

Try converting Code as a Property instead of a member and then binding it

Related

Winforms combobox displaying class names instead of actual object name

I have this class that contains a static list
public class TypeList
{
public string Name;
public string NameTag;
public TypeList(string Name, string NameTag)
{
this.Name = Name;
this.NameTag = NameTag;
}
public static List<TypeList> DataType = new List<TypeList>() {
new TypeList("DataType","-1"),
new TypeList("OpOne","1"),
new TypeList("OpTwo","2"),
};
}
I then put the static list called DataType into a combobox:
public void RefreshList()
{
List<TypeList> data = new List<TypeList>();
data = TypeList.DataType;
typeCB.DataSource = data;
typeCB.DisplayMember = "Name";
typeCB.ValueMember = "NameTag";
typeCB.SelectedValue = -1;
typeCB.SelectedText = "Select DataType";
}
However, when I run it, all I get are the classnames in my combobox. Is something wrong with my code? I tried to do
data.Select(x=>x.Name).ToList()
But that just gives me the name portion.
I might be wrong, but based on the Documentation and Example it might be that this Feature only works with public property getters, not public fields:
Gets or sets the property to display for this ListControl.
public class USState
{
private string myShortName;
private string myLongName;
public USState(string strLongName, string strShortName)
{
this.myShortName = strShortName;
this.myLongName = strLongName;
}
public string ShortName
{
get
{
return myShortName;
}
}
public string LongName
{
get
{
return myLongName;
}
}
}
Of course I would also advise against making the list a part of the Type class. A simple Programm scope static would be better. If that is the case and as autoproties have have become a thing by now, this should be enough of a fix:
public class Type
{
public string Name { private set; get } ;
public string NameTag {private set; get };
public TypeList(string Name, string NameTag)
{
this.Name = Name;
this.NameTag = NameTag;
}
}
//use in the class of main, the form or some similar central point
static List<Type> TypeList = new List<Type>();

DisplayMembers in ListBox - Not displaying anything

In Form1, why isn't myListBox displaying my name property inside of class Student
Form1
private static List<Student> studentListHome = new List<Student>();
...
public void BindData()
{
if (studentListHome != null)
{
studentListBox.DataSource = studentListHome;
studentListBox.DisplayMember = "name";
}
}
private void refreshButton_Click(object sender, EventArgs e)
{
BindData();
}
Student
private string name;
public string Name
{
get
{
return name;
}
}
...
public Student(string _name, int _id, string _bday)
{
name = _name;
id = _id;
bday = _bday;
}
Because 'name' is a private string.
Try using 'Name' which is your property.
studentListBox.DisplayMember = "Name";
This fixed it
Changing
studentListBox.DisplayMember = "Name";
To
foreach(Student s in studentListHome){
studentListBox.DisplayMember = "Name";
}

Inserting a list into a listbox in C#

I have made a list and the data is added into the list by the user using textboxes and comboboxes, I am now trying to enter this list of data into a listbox but everytime I try to add the data I get the output as the class name e.g WindowApplicaion.Journey or it comes out as System.Collections.Generic.List`1[WindowApplication.Journey], i'm not sure if this is due to me placing the conversion code in the wrong place or i'm just doing it all wrong, here is my code for both cases:
private void ShowAllToursbttn_Click(object sender, RoutedEventArgs e)
{
foreach (Tour t in company.tours)
{
string converter = company.tours.ToString();
ToursListBox.Items.Add(converter);
}
}
or
private void ShowAllToursbttn_Click(object sender, RoutedEventArgs e)
{
foreach (Tour t in company.tours)
{
string ConvertedList = string.Join(" ", company.tours);
TourListBox.Items.Add(ConvertedList);
}
}
Where tours is my list I created in my company class and t is each instance in the list, any advice would be great, thank you!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowApplication
{
class Tour
{
private string custFirstname;
private string custSurname;
private string custAddress;
private string pickupArea;
private string pickupDateandTime;
private string pickupDescription;
private string destinationArea;
private string destinationDescription;
//Creating getters and setters for each attribute
#region getters/setters
public string firstname
{
get { return custFirstname; }
set { custFirstname = value; }
}
public string surname
{
get { return custSurname; }
set { custSurname = value; }
}
public string address
{
get { return custAddress; }
set { custAddress = value; }
}
public string pickuparea
{
get { return pickupArea; }
set { pickupArea = value; }
}
public string pickupdateandtime
{
get { return pickupDateandTime; }
set { pickupDateandTime = value; }
}
public string pickupescription
{
get { return pickupDescription; }
set { pickupDescription = value; }
}
public string destinationarea
{
get { return destinationArea; }
set { destinationArea = value; }
}
public string destinationdescription
{
get { return destinationDescription; }
set { destinationDescription = value; }
}
}
}
That is my Tour class.
private void AddThisTourbttn_Click(object sender, RoutedEventArgs e)
{
Tour t = new Tour();
t.firstname = CustomerFirstnameTxt.Text;
t.surname = CustomerSurnameTxt1.Text;
t.address = CustomerAddressTxt.Text;
t.pickupdateandtime = TourDateTimeTxt.Text;
t.pickuparea = TourPickupArea.Text;
t.pickupescription = TourPickupDescriptionTxt.Text;
t.destinationarea = TourDestinationArea.Text;
t.destinationdescription = TourDestinationDescriptionTxt.Text;
company.addTour(t);
}
and on my MainWindow I have assigned each textbox to its appropriate get/set.
Your program displaying class name in listbox because you use default object's toString() method in ShowAllToursbttn_Click, which will output class name. Try to override ToString() method in Tour class, to output string with your desired format for example:
public override string ToString()
{
return String.Format("Firstname: {0}; Surname: {1}", firstname, surname);
}
And change ShowAllToursbttn_Click logic to:
private void ShowAllToursbttn_Click(object sender, RoutedEventArgs e)
{
foreach (Tour t in company.tours)
{
TourListBox.Items.Add(t.ToString());
}
}

Use only list/collection to store data locally on webpage

I'm getting only 1 record using this code, but i want to display multiple records on page. I have 3 columns to display on page which are: id,name and lastname.
How can I get this done?
Code Behind:
protected List<Class1> GetClass1()
{
Class1 uinfo = new Class1();
uinfo.ID = Convert.ToInt16(TextBox1.Text);
uinfo.Name = TextBox2.Text;
uinfo.LastName = TextBox3.Text;
data.Add(uinfo);
return data;
}
protected void BindUserDetails()
{
data = GetClass1();
GridView1.DataSource = data;
GridView1.DataBind();
}
Class file:
public class Class1
{
Int16 id;
string name = string.Empty;
string lastname = string.Empty;
public Int16 ID
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
}
I got one solution for you. This is just a sample implementation. Please modify according your requirement.
The Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace BindingListSample
{
public partial class _Default : System.Web.UI.Page
{
static List<Employee> bindingL = new List<Employee>();
protected void Btn_Click(object sender, EventArgs e)
{
bindingL.Add(new Employee { Name = TxtName.Text });
GrvSample.DataSource = bindingL;
GrvSample.DataBind();
}
}
public class Employee
{
public string Name { get; set; }
}
}
The problem is that you need to use static with your list. When you use static you can store the values that you have inserted until the application is closed. Refer Static Keyword for more explanation.
This is just my way to tackle this situation.
Hope This Helps
`List<Class1> data = new List<Class1>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
if (Session["test"] != null)
{
data = Session["test"] as List<Class1>;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Class1 cl = new Class1 { ID = Convert.ToInt16(TextBox1.Text), Name = TextBox2.Text, LastName = TextBox3.Text };
data.Add(cl);
Session["test"] = data;
dataBind();
}
protected void dataBind()
{
GridView1.DataSource = data;
GridView1.DataBind();
}
}
}`

Initializing an object property to the value of another object property in DataGridView

I'm attempting to bind two objects (List LedgerEntries and List BuyerSellers) to a single DataGridView. LedgerEntry contains a property for Buyer_Seller, and I would like the end-user to select a Buyer_Seller from a combobox (populated by the BuyerSellers generic collection) in the DataGridView and the LedgerEntries string BuyerSeller property be set to the Buyer_Seller string Name property.
At the moment I'm only using one BindingSource and I'm not defining my own columns; they are auto-generated based on the object being bound to the DGV. Where I'm a little lost is how to ensure the property in one object is initialized to the value of the combobox that's populated by another object. Thanks in advance for any help.
Found what I was looking for here: http://social.msdn.microsoft.com/Forums/vstudio/en-US/62ddde6c-ed96-4696-a5d4-ef52e32ccbf7/binding-of-datagridviewcomboboxcolumn-when-using-object-binding
public partial class Form1 : Form
{
List<LedgerEntry> ledgerEntries = new List<LedgerEntry>();
List<Address> addresses = new List<Address>();
BindingSource entrySource = new BindingSource();
BindingSource adSource = new BindingSource();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
entrySource.DataSource = ledgerEntries;
adSource.DataSource = addresses;
DataGridViewComboBoxColumn adr = new DataGridViewComboBoxColumn();
adr.DataPropertyName = "Address";
adr.DataSource = adSource;
adr.DisplayMember = "OrganizationName";
adr.HeaderText = "Organization";
adr.ValueMember = "Ref";
ledger.Columns.Add(adr);
ledger.DataSource = entrySource;
addresses.Add(new Address("Test1", "1234", 5678));
addresses.Add(new Address("Test2", "2345", 9876));
}
private void button1_Click(object sender, EventArgs e)
{
foreach (LedgerEntry le in ledgerEntries)
MessageBox.Show(le.Address.OrganizationName + " // " + le.Description);
}
}
public class LedgerEntry
{
public string Description { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string OrganizationName { get; set; }
public string StreetAddress { get; set; }
public int ZipCode { get; set; }
public Address(string orgname, string addr, int zip)
{
OrganizationName = orgname;
StreetAddress = addr;
ZipCode = zip;
}
public Address Ref
{
get { return this; }
set { Ref = value; }
}
public override string ToString()
{
return this.OrganizationName;
}
}

Categories