Dev component Lookupedit,comboboxedit binding data source

Source: Internet
Author: User



Lookupedit can be bound to a data table (DataTable) or Object Data Group (objects List) as a data source, and a drop-down form can customize the display of fields.

Binding a data source requires setting three parameters: DisplayMember, Valuemember,datasource.

DisplayMember: Displays the member name (or field name) of the data in the Lookupedit input box. If the customer name is displayed, set to CustomerName.

ValueMember: the member name (or field name) of the value, usually the primary key or business primary key that identifies the column in the drop-down window. If the customer number is set to Customercode

DataSource: Data source. A data table (DataTable) or an Object Data group (objects List).



Comboboxedit cannot directly bind a data source, nor can it display multiple fields. You need to assign a value to Properties.items manually, refer to the code below.





C # Code:


 Binding the data source of the Lookupedit component
LookUpEdit1.Properties.DisplayMember = "ColorCode";
LookUpEdit1.Properties.ValueMember = "ColorCode";
LookUpEdit1.Properties.DataSource = This . Colordata;


Manually add a Comboboxedit data source
ComboBoxEdit1.Properties.Items.Clear ();

foreach (DataRow row in this .) Colordata.rows)
{
COMBOBOXEDIT1.PROPERTIES.ITEMS.ADD (row["ColorCode"]);
}


Source: C/S Framework Network (www.csframework.com) qq:1980854898








C # Code: 

 Private DataTable Colordata
{
Get
{

DataTable Dtcolor =New DataTable ();
DTCOLOR.COLUMNS.ADD ("ColorCode",typeofstring)); 
      dtcolor.columns.add ("ColorType",  typeof (string)); 
      dtcolor.rows.add (new  object[] {"Red", "Color1"}); 
      dtcolor.rows.add (new  object[] {"Green", "Color1"}); 
      dtcolor.rows.add (new  object[] {"Blue", "Color2"}); 
      dtcolor.acceptchanges (); 
       
       return dtcolor; 
   } 



//Source: C/S frame net (www.csframework.com) 
____________________________________________________________________
To bind a data source:
Lookupedit.properties.valuemember = The actual field to use; Equivalent to EditValue
Lookupedit.properties.displaymember = The field to be displayed; Equivalent to Text
Lookupedit.properties.datasource = data source;
To determine whether to select a drop-down box:
if (This.lookupedit.editvalue==null | | this.lookupedit.editvalue.tostring () = = "Nulltext")
{
Prompt message stating that the drop-down box is not selected
}
Empty the Nulltext value:
Lookupedit.properties.nulltext=null;



Set the Nulltext value:
lookupedit.properties.nulltext= "Please select";
Use the value of lookupedit1:
Variable =this.lookupedit.editvalue.tostring ()//is the value of Ookupedit.properties.valuemember
Variable =this.lookupedit.text.trim ()//is the value of Ookupedit.properties.displaymember





It is particularly noteworthy that sometimes we have to use Lookupedit to achieve some of the effects of Combox, in the actual use of the program when loading the default selection of the first item, its settings are:





lookupedit.itemindex=0;//Select the first item


Lookupedit.itemindex=-1;//no option, this shows the Nulltext value in fact this place as long as Editvalue==null,lookupedit show Nulltext


lookupedit1.editvalue=value;//automatically searches for datasouse, selects the value that matches it, assigns null without a value, and value must be the same as the data type of ValueMember.


In the GridView you select a record, then you can use Bindingsourse.current to get the data you want to select, if the entity class can (object) to cast, and then do some other operations.



However, lookupedit when you select a record, the value of Bindingsourse.current is always the first record, and to get the record of your chosen data source, you must use Lookupedit.getdatasoursebykeyvalue ( KeyValue), (object) is forced to go this way. But you should be aware that keyvalue must be the primary key in your data source, otherwise you get the first record of the same key. What if I encounter a multi-key value to determine a record?



This problem Dev Express also has his corresponding solution, is to use the Gridlookupedit, it is the GridView and Lookupedit of the Union bar, using this you can choose the primary key of the same record, The use of Gridlookupeditview.getdatasourcerowindex (Gridlookupeditview.focusedrowhandle) is possible. Instance Binding method


//Bind lookupedit 
        private void Bounddata () 
         {
            string sql = " Select Daid,xm,sfzh from Baseinfo "; 
            DataSet ds = Archives.DataAccess.DataSql.Query (SQL); 
            if (ds! = null) 
             {
                 int rows = ds. Tables[0]. Rows.Count;





Lookupedit1.editvalue = "Daid";
LookUpEdit1.Properties.ValueMember = "Daid";
LookUpEdit1.Properties.DisplayMember = "XM";
LookUpEdit1.Properties.DataSource = ds. Tables[0];





Self-adapting width
LookUpEdit1.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;





               //Fill columns 
                 LookUpEdit1.Properties.PopulateColumns ();





               //Set column Properties 
                // Lookupedit1.properties.columns[0]. Visible = false; 
                LOOKUPEDIT1.PROPERTIES.COLUMNS[1]. Caption = "name"; 
                LOOKUPEDIT1.PROPERTIES.COLUMNS[2]. Caption = "Social Security Number"; 
                LOOKUPEDIT1.PROPERTIES.COLUMNS[1]. Width = 120; 
                LOOKUPEDIT1.PROPERTIES.COLUMNS[2]. Width = +;





Control the total width of the selection
LookUpEdit1.Properties.PopupWidth = 500;





Column format settings
Lookupedit1.properties.columns[0]. FormatString = "000000";





Select the first item
Lookupedit1.itemindex = 0;
}
}





Value Change Event
private void Lookupedit1_editvaluechanged (object sender, EventArgs e)
{
String daid = This.lookUpEdit1.EditValue.ToString (); Is the value of the OokUpEdit.Properties.ValueMember
String xm = This.lookUpEdit1.Text.Trim ();
}





Allow input of custom values



LookupEditor1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;




1 // Manually add the data source of ComboBoxEdit
2 comboBoxEdit1.Properties.Items.Clear ();
3
4 foreach (DataRow row in this.ColorData.Rows)
5 {
6 comboBoxEdit1.Properties.Items.Add (row ["ColorCode"]);
7}


3. Set the comboBoxEdit drop-down box to be read-only and not allowed to edit. Just set the TextEditStyle of its Properties to DisableTextEditor. 



Dev Component Lookupedit,comboboxedit binding data source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.