How to populate or fill a Devexpress lookUpEdit control with LINQ to SQL

Ingredients:
  • Developer Express 2010 or above
  • A form containing the Devexpress LookUpEdit control
  • VS 2008 with CSharp as the programming language
  • SQL Server 2005 with a database containing the table displayed in the LookUpEdit control
Steps:
Open your Solution Explorer, right-click your Project and choose Add new item.

In the new popup window, choose Data tab and 'LINQ to SQL Classes'. You can give it any name you like: the default name is DataClasses1 and click Add button.
In the 'Object Relational Designer' window click the 'Server Explorer' link to open the 'Server Explorer' pane of VS 2008.
Right-click the Data Connections and choose 'Add Connection'.
In the connection window you can enter necessary information such as 'Server name', database name, and so on to connect to your SQL Server 2005 database.

Click the plus button near the tables tab in the Server Explorer pane to display all the tables in your database. Drag all these tables to the 'Object Relational Designer' window.

Go to your formName.cs and add the following code:
public partial class FrmName : DevExpress.XtraEditors.XtraForm {
DataClasses1DataContext dc = new DataClasses1DataContext();
public FrmName() {
InitializeComponent();
}
private void FrmName_Load(object sender, EventArgs e){
// LINQ
var variableTableName = from canBeAnyName in dc.YourTableNames select new
{ canBeAnyName.firstColumnName, canBeAnyName.secondColumnName, canBeAnyName.thirdColumnName};
// Remember to add s after YourTableName in the above code
yourLookUpEditControl.Properties.DataSource = variableTableName;
yourLookUpEditControl.Properties.DisplayMember = "yourColmnNameToBeDisplayed";
//can be firstColumnName for example.
yourLookUpEditControl.Properties.ValueMember = "yourColmnName containing the primary key";
}
}
Switch to the design view of your form, locate your LookUpEdit control, right-click and choose Properties. Or you can also left-click to choose the LookUpEdit control and press F4 to open the properties window.

In the properties window of your LookUpEditControl locate the Columns property. Click the ... button on the right of this property to open the LookUpColumnInfoCollectionEditor window.


In this Editor you can add new column into your LookUpEdit control. The two most important properties to be used are Caption and FieldName. Caption is the name of the column in your LookUpEdit control that will be displayed to users and FieldName is the name of the column in your SQL Server 2005 database table.

Notes:
  • These lines of codes:
    // LINQ
    var variableTableName = from canBeAnyName in dc.YourTableNames select new
    { canBeAnyName.firstColumnName, canBeAnyName.secondColumnName, canBeAnyName.thirdColumnName};
    can be replaced with
    var variableTableName = dc.GetTable<YourTableName>();

No comments:

Post a Comment

Blog Archive

About Me

My photo
I am a software developer with roughly 5 years of experience in developing end-to-end solutions in C#, Java, C/C++, PHP and HTML/CSS/Javascript. At the moment, I am joining the Professional Doctorate in Engineering degree program in Software Technology at Eindhoven University of Technology. My areas of particular interest include software design, data structures and algorithms, problem solving, software security, embedded system, machine learning, and data science.