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


Top 5 Contributors of the Month
david stephan

Home >> Forum >> WPF >> Post New QuestionBookmark and Share Subscribe to Forum

Show the Default Text Of Combobox as "--Select--" when combo is binding to dataset.

Posted By: Aamrapali Tamgadge     Posted Date: February 25, 2013    Points:5   Category :WPF
Hello Sir i have bind my combobox to one ds to show the disply text,and value text by usng follwing method in my class,i have tried a lot to disply the default text but not getting he result.pls suggest me the way to show it.

public void BindComboBox(SqlConnection connection,CommandType commandType, string commandText,ComboBox combobox,
string textField, string valueField, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);

SqlDataAdapter da = new SqlDataAdapter(cmd);
ds.Tables.Clear();
da.Fill(ds);
cmd.Parameters.Clear();
connection.Close();
//List<myClass> myList = new List<myClass>();
combobox.ItemsSource = ds.Tables[0].DefaultView;
combobox.DisplayMemberPath = textField;
combobox.SelectedValuePath = valueField;

//combobox.Text = "Select";
}


Responses
Author: priya             
Posted Date: February 25, 2013     Points: 20   

you can try this code. after finished loading with data in combo box write below code

combobox.Items.Insert(0,"--Select--");

Author: Aamrapali Tamgadge             
Posted Date: February 25, 2013     Points: 20   

hello priya mam i have tried this but if i put this line then it gives the following error

{"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."}

and i have to use both value field and textfiel.

Software Programmer At OTIS ISRC
Author: priya             
Posted Date: February 26, 2013     Points: 20   

Hi,

Sorry the above solution is for windows form or asp.net forms.

below code you can use for WPF

<ComboBox Name="combobox"
IsEditable="True"
IsReadOnly="True"
Text="-- Select--" />


Author: Aamrapali Tamgadge             
Posted Date: February 27, 2013     Points: 20   

no mam this is not my requirement.i hv tried this.

Software Programmer At OTIS ISRC
Author: Aamrapali Tamgadge             
Posted Date: March 05, 2013     Points: 20   

i got the solution.

DataRow dr = dt.NewRow();
dr[textField] = "--Select--";
dt.Rows.InsertAt(dr, 0);
combobox.DataContext = dt;
combobox.DisplayMemberPath = textField;
combobox.SelectedValuePath = valueField;


Software Programmer At OTIS ISRC
Author: Andrew Hudson             
Posted Date: July 23, 2013     Points: 20   

Hi thanks for sharing a useful information this code is very helpful for me.



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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