.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Using .Net 4.0 Optional and Positional Parameters to bind a dropdown

Posted By:shankey       Posted Date: June 02, 2011    Points: 200    Category: .Net Framework    URL: http://www.dotnetspark.com  

Perform following actions using Optional and Positional Parameters of a common function -Sorting -Adding custom row -Selecting item

Main screen

Common function to fill combo box


public static void FillCombo(DropDownList dropDown, DataTable dtDropDown, string dataTextField, string dataValueField, int selectedItemValue = 0, string selectedItemText = "", bool isSort = false, SortOrder sortExpression = SortOrder.Ascending, bool addCustomFirstRow = false, string customRowText = "Please select", string customRowValue = "")
            /// Checking whether dropdown and DataTable is not null.
            if (dropDown != null && dtDropDown != null)
                ///DataTable contains at least one row
                if (dtDropDown.Rows.Count > 0)
                    ///Checking whether Sorting is to be done.
                    if (isSort)
                        string sordOrder = (SortOrder.Ascending == sortExpression ? " ASC" : " DESC");
                        dtDropDown.DefaultView.Sort = dataTextField + sordOrder;
                    ///Defining Data Text Field and Data Valu Field
                    dropDown.DataTextField = dataTextField;
                    dropDown.DataValueField = dataValueField;
                    ///Binding dropdown control 
                    dropDown.DataSource = dtDropDown;
                    ///Checking whether A custom row is to be added.
                    if (addCustomFirstRow)
                        dropDown.Items.Insert(0, new ListItem(customRowText, customRowValue));
                    ///Checking whether there is any selected item based on item value that need to be shown.
                    if (selectedItemValue != 0)
                        dropDown.Items.FindByValue(Convert.ToString(selectedItemValue)).Selected = true;
                    ///Checking whether there is any selected item based on item text that need to be shown.
                    if (!String.IsNullOrEmpty(selectedItemText))
                        dropDown.Items.FindByText(selectedItemText).Selected = true;

Sort in ascending order

Sort in descending order

Add new row (Custom row)

·  Entered item is added to the combo box

·  Verifying whether entered item value is set as option value

Set Selected item based on text entered

  Based on entered text, Item is set selected in combo box

Set Selected item based on value entered

You can also download the sample application from below link

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend