I'm trying to get the LookUpEdit to show an initial value when the form is displayed. I'm binding a list of countries as the datasource and then setting the EditValue when the form loads which should show the country as the selected item in the LookUpEdit. Unfortunately, it just shows an empty value. The LookUpEdit appears to otherwise work and allows me to scroll through the list of countries and select an item and the value is passed back when I submit the form.
The Country class:
public class Country
{
public Country();
public int CountryId {get; set;}
public string CountryName {get; set;}
public string IsoCode {get; set; }
}
The code behind the form containing the LookUpEdit:
this.country.Properties.DataSource = this.Countries;
this.country.Properties.DisplayMember = "CountryName";
this.country.Properties.ValueMember = "CountryId";
this.country.EditValue = initialCountry;
this.country.DataBindings.Add("EditValue", viewModel.Address, "AddressCountry", false, DataSourceUpdateMode.OnPropertyChanged);
In this example this.Countries is a populated List<Country> and initialCountry is set to an instance of Country and viewModel.Address contains a property Country AddressCountry.
I've tried both setting the EditValue directly only and setting the data binding to EditValue on it's own as well. Whatever I try, the LookUpEdit is always blank when the form loads and I need it to be set to the initialCountry. I'm sure it's something really simple but I'm not seeing it so any help is much appreciated.
In addition to the Marko's answer:
There is a special mode of data binding to the entire business objects in lookup:
this.country.Properties.DataSource = this.Countries;
this.country.Properties.DisplayMember = "CountryName";
this.country.Properties.KeyMember = "CountryId";
this.country.EditValue = initialCountry;
This mode allows the lookup mechanism to find a match between the editor's value (a Country business object) and another Country business objects in the lookup data source, via the key field ("CountryId") is assigned to the RepositoryItemLookUpEditBase.KeyMember property.
Here are some additional benefits of this mode:
you can use multiple key fields ("composite/compound key" feature);
// field names delimited with the ';' character
this.city.Properties.KeyMember = "CountryId;RegionId;CityName";
you can match the business objects, loaded from the separate data-contexts, and use all the advantages of lazy-loading approach:
// The CountryId value is enough for match.
// All the another fields(e.g. CountryName) can be skipped while loading
this.country.EditValue = new Country() { CountryId = 5 }
You should not set this.country.EditValue to an instance of Country, but to CountryId, since this is your ValueMember.
this.country.EditValue = initialCountry.CountryId;
EDIT: if you want to get the selected object you should use GetDataSourceRowByKeyValue
var selectedCountry = this.country.GetDataSourceRowByKeyValue(this.country.EditValue) as Country;
Edit Value requires Data with Exact Data Type as it in Database,
so assign data with convert function of appropriate data type
Related
I have a Struct that looks like this:
public struct Server
{
public String ServerName, ServerUrl, ServerEnvironment;
};
Then I create a List of Structs:
List<Server> MyServers = new List<Server>();
...and read about eight records into it from a small XML file. This is working well, and if I hover over "MyServers" in the debugger, it looks something like this:
MyServers Count=8
[0] {MyApp.Server}
ServerEnvironment "DEV"
ServerName "My Dev Server #1"
ServerUrl "https://mydev1.mycompany.com/"
[1] {MyApp.Server}
etc...
Then if I do something like MessageBox.Show(MyServers[0].ServerName);, it displays the expected value.
Now what I would like to do is create a ComboBox from the ServerName fields. When the user selects one, I think I would then use the SelectedIndex property of the ComboBox to access the other information (ServerUrl and ServerEnvironment) for the selected ServerName.
I thought I could do something like this:
comboBoxServers.DataSource = MyServers ... something ... ServerName;
But I can't seem to find anything that works. Is this even possible, or do I need to create a separate, simple List with only the ServerName strings and use that for the ComboBox DataSource?
You can use only DisplayMember to show names as Text in combobox.
Then comboBox.SelectedValue will return whole object.
But for using DisplayMember you need change field ServerName to the property
public struct Server
{
public string ServerName { get; set; }
}
comboBoxServers.DisplayMember = "ServerName";
comboBoxServers.DataSource = MyServers;
If you don't want change fields of struct to the property you can override ToString method for your struct.
ComboBox simply calling .ToString() on every item in the datasource, if DisplayMember not assigned, for generating Text of item.
public override string ToString()
{
return Name;
}
Then you can access selected server information through comboBoxServers.SelectedValue which return whole instance of Server
var selectedServer = (Server)comboBoxServers.SelectedValue;
selectedServer.ServerUrl;
selectedServer.ServerEnvironment; // ...
Create List Server:
List<Server> Servers= new List<Server>()
{
new Server {ServerEnvironment = "DEV", ServerName = "My Dev Server #1"},
new Server {ServerEnvironment = "DEV1", ServerName = "My Dev Server #2"},
};
Then add list to comboBox:
comboBoxServers.DataSource = Servers;
comboBoxServers.ValueMember = "ServerEnvironment ";
comboBoxServers.DisplayMember = "ServerName" ;
This might do the trick for you
comboBoxServers.ValueMember = ServerUrl;
comboBoxServers.DisplayMember = ServerName;
comboBoxServers.DataSource = MyServers;
DisplayMember: Gets or sets the property to display for any ListControl. The String specifying the name of an object property that is contained in the collection specified by the DataSource property.
ValueMember: Gets or sets the path of the property to use as the actual value for the items in the ListControl. This String representing a single property name of the DataSource property value, or a hierarchy of period-delimited property names that resolves to a property name of the final data-bound object.
So now when the user selects one in the comboBoxServers you can access the other information (ServerUrl and ServerEnvironment) for the selected ServerName like
string urlenv = comboBoxServers.SelectedValue;
string serName = comboBoxServers.Text;
I've simple class called "Order".
class Order {
public string ID { get;set;}
public string Something {get;set;}
.... more fields...
}
I then assign list of orders into DataSource of GridControl.
List <Order> ListOfOrders = new Order();
gridControl.DataSource = ListOfOrders;
I've added a CheckBoxRowSelect option in GUI so that user can choose rows by selecting the checkbox. What value do I need to add to class Order so that any checkbox change in that GridControl gets updated in instant in it's own field so that the ListOfOrders is always up to date and has that checkbox value stored so it can be processed?
I've made similar thing with ComboBox inside one of the columns and it seems to work instantly without any additional change on my part.
var columnKurier = view.Columns.AddField("Courier");
columnKurier.ColumnEdit = riCombo;
columnKurier.VisibleIndex = 0;
columnKurier.OptionsColumn.AllowFocus = true;
I simply had to add Courier field into Order class and that's it. But i don't know which "field name" should it be for checkbox and whether it will get the same behaviour as for the ComboBox.
Add in your object order :
public Boolean Mark{get;set;}
and in your your form contructor :
InitializeComponent();
new GridCheckMarksSelection(gridControl);
i have atach a class GridCheckMarksSelection you must add it in your project.
MarkClass
I hop that you want
You can not add the option CheckBoxRowSelect to GridControl, and just use a bool field in your Class Order, it should be displayed as checkbox in GridControl.
Consider the following text as two lists(separated with ':'), as you can see the second rows are always unique but items in the first row can be repetitive;
Book:m234
Clover:h67
Pencil:a12
Book:x67
I want to populate a listbox with items in the first column(Book, Clover, ...) but the problem is that when I'm going to retrieve the selected item in the listbox, I can't be sure about it's respective value in second column. (for example in case of 'Book');
NOTE: I'm not looking for any workaround for solving this problem because there are many. What I want to know is that:
Is it possible to pass and object to ListBox.Items.Add() in a way that the object carries two values(each value/property for each column) and in time of getting the selected item, we would have an object with the two values(maybe as the properties of the object)?
Is such a thing possible in C#? (.NET 4.5)
You can pass objects that pair data with names to your ListBox, and control what gets displayed and what gets returned back to you by using DisplayMember and ValueMember:
class ListBoxItem {
public string DisplayName {get;set;}
public string Identifier {get;set;}
}
...
ListBox.Items.Add(new ListBoxItem {
DisplayName = "Book", Identifier = "m234"
});
ListBox.Items.Add(new ListBoxItem {
DisplayName = "Clover", Identifier = "h67"
});
ListBox.Items.Add(new ListBoxItem {
DisplayName = "Pencil", Identifier = "a12"
});
ListBox.Items.Add(new ListBoxItem {
DisplayName = "Book", Identifier = "x67"
});
ListBox.DisplayMember = "DisplayName";
ListBox.ValueMember = "Identifier";
Now your list box displays the same list of strings, but the values returned for end-user selections would be different.
I'm using SubSonic 2.2 for my DAL and extended one of my classes with a calculated property that returns a string containing another property with indenting based on the level of the outline at which the item occurs. The code for the property is below. The problem is that when I try to use this property as the DisplayMember for a ListBox control on a form (the reason I wrote it in the first place) it won't work. The ListBox reverts to displaying the ID property which is set as the ValueMember. To test that the property was working I looped through the collection of objects that I was populating the ListBox with and, using MessageBox.Show(obj.property), confirmed that it was indeed returning the value I'm looking for. Am I missing something or should this work? btw - There may be a better way to do the indenting but that's not what I'm after at the moment, thanks!
Code follows:
public partial class InteriorsCategory : ActiveRecord, IActiveRecord
{
public string ListDisplay
{
get
{
string returnValue = "";
for (int i = 1; i < this.SpecLevel; i++)
{
returnValue += " ";
}
returnValue += this.CategoryName;
return returnValue;
}
}
}
<>
I definitely get data in my collection and the binding I'm doing is exactly the same as yours (binding code posted below). The return value of the ListDisplay property that I'm using is a string concatenation of two values in the object. Think of it as a "full name" property that concatenates the FirstName a space and the LastName properties into a single string which it returns. I am trying to bind the ListDisplay property to the DisplayMember property of the listbox, but all that shows in the listbox is the Id field which I am binding to the ValueMember.
private void FillCategories()
{
lstPackageCategories.DataSource = new InteriorsCategoryCollection().Load();
lstPackageCategories.DisplayMember = "CategoryName";
lstPackageCategories.ValueMember = "Id";
((InteriorsCategoryCollection)(lstPackageCategories.DataSource)).Sort("SpecSection", true);
lstPackageCategories.SelectedItem = lstPackageCategories.Items[0];
currentCategory = (InteriorsCategory)lstPackageCategories.SelectedItem;
RefreshAvailableItems();
}
If your able to see your data in the collection, then it sounds like there is a problem on the binding of your ListBox. Here is an example of how I bind a ListBox using a SubSonic collection of values.
ISOCountryCodeCollection countrys =
new ISOCountryCodeCollection().OrderByAsc(ISOCountryCode.Columns.Country).Load();
Country.DataSource = countrys;
Country.DataValueField = "ThreeChar";
Country.DataTextField = "Country";
Country.DataBind();
In the example above, I'm binding the 3 character country code to the "DataValueField" and the full country name to the "DataTextField".
My project display's program names from the database into dropdownlist and each program has a ID. I want to display both name and id in the dropdownlist so that they can be differenciated from each other.
eg: 'california lifeline (CLA)'
where 'california lifeline' is the name of the program and the id is 'CLA'. I have created a stored procedure which displays data based on the program ID.
This the code of my dropdownlist control.
private void LoadProgramName()
{
_drp_program = (DropDownList)Page.FindControl("bodyuc$drp_program");
dsprg = rProxy.GlobalFetchFromDB(strCountyName, "DBO.oea_sp_get_onoff_programNames");
_drp_program.DataSource = dsprg;
_drp_program.DataTextField = "PROG_NAME";
_drp_program.DataValueField = "PROGRAM_ID";
_drp_program.DataBind(); ;
ListItem lst_prog = new ListItem();
lst_prog.Value = "";
lst_prog.Text = "--Select One--";
_drp_program.Items.Insert(0, lst_prog);
_drp_program.Items.Insert(1, "ALL");
}
BTW, the dropdown is a part of pagecontrol.
Help is Appriceated.
If your GlobalFetchFromDB() returns instances of an object which you can "extend" as a partial class, then I'd add a "helper" property to the class that formats your displayed string.
One advantage to this is that the format can be dependent on the values of other properties in the instance.
For example:
public partial class ProgramItem
{
public string DisplayName { get { return PROG_NAME.ToUpper(); } }
}
Then use _drp_program.DataTextField = "DisplayName";
An alternative is to construct a class that encapsulates the returned items and adds the DisplayName as a decorator.
Instead of binding _drp_program to dsprg, loop through the results in dsprg and add each item individually.
foreach(dsprgObject in dsprg)
{
_drp_program.Items.add(new ListItem(dsprgObject .ColA + " " + dsprgObject .ColB, dsprgObject.PROGRAM_ID));
}
You can try
_drp_program.DataSource = from item in dsprg
select new
{
PROG_NAME = string.Format("{0}({1})", item.PROG_NAME, item.PROGRAM_ID) ,
PROGRAM_ID = item.PROGRAM_ID
};
instead of
_drp_program.DataSource = dsprg;
Usually the fastest way is to concatenate ID and NAME in an SQL query (but you're using an sp so that's a bit difficult). Also, since you binding your control in code, you can concatenate ID and Name in code and populate your dropdownlist manually (either by calling Items.Insert or by binding your dropdown to a collection which members have a field/property that contains the concatenated value). AFAIK, you can only specify a field/property name for DataTextField and DataValueField, no expressions etc...