Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
By wizard i bind my combobox to database column but i dont want to show last element of my column(last row).please help
Through programing i dont know how to bind combobox. please answer this tooWink | ;-)
Posted 16-Jan-13 4:05am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To bind the combobox through code - you would do something like:
 
combobox.datasource = yourdata;
combobox.DataValueField = "FieldFromYourDataToReturn";
combobox.DataTextField = "FieldFromYourDataToShow";
 

To not show an element - you could just remove it like below:
combobox.Items.RemoveAt(combobox.Items.Count-1);
Or you could make sure that it was never selected - which would probably be a better solution.
  Permalink  
Comments
Muhamad Faizan Khan at 16-Jan-13 9:18am
   
private void comboBoxSurahName_SelectedIndexChanged(object sender, EventArgs e)
{
//if(comboBoxSurahName.SelectedIndex==114)
comboBoxSurahName.Items.RemoveAt(comboBoxSurahName.Items.Count - 1);

}
 
this is not working ;-(
grimbeast at 16-Jan-13 9:51am
   
Have just tested below code, and it works without errors.
Please post whatever error you receive.

private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\v11.0;Initial Catalog=SomeDB;Integrated Security=True");
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM Table", conn);
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr["Message"]);
}
conn.Close();
}
 
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.RemoveAt(comboBox1.Items.Count - 1);
}
Muhamad Faizan Khan at 16-Jan-13 23:50pm
   
Error on this line: comboBoxSurahName.Items.Add(dr["SNEnglish"]);
 
Items collection cannot be modified when the DataSource property is set.
 
while the over all code is this:
 
private void Read_QuranoTafsir_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'quranDBdetailsDataSet1.Surah' table. You can move, or remove it, as needed.
this.surahTableAdapter1.Fill(this.quranDBdetailsDataSet1.Surah);
// TODO: This line of code loads data into the 'quranDBdetailsDataSet.Quran' table. You can move, or remove it, as needed.
//this.quranTableAdapter.Fill(this.quranDBdetailsDataSet.Quran);
// TODO: This line of code loads data into the 'quranDBdetailsDataSet.Translations' table. You can move, or remove it, as needed.
this.translationsTableAdapter.Fill(this.quranDBdetailsDataSet.Translations);
// TODO: This line of code loads data into the 'quranDBdetailsDataSet.Tafseers' table. You can move, or remove it, as needed.
this.tafseersTableAdapter.Fill(this.quranDBdetailsDataSet.Tafseers);
// TODO: This line of code loads data into the 'quranDBdetailsDataSet.Surah' table. You can move, or remove it, as needed.
this.surahTableAdapter.Fill(this.quranDBdetailsDataSet.Surah);
 
OleDbConnection conn = new OleDbConnection(conStr);
conn.Open();
OleDbCommand comm = new OleDbCommand("SELECT * FROM Surah", conn);
OleDbDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
 
comboBoxSurahName.Items.Add(dr["SNEnglish"]);
}
conn.Close();
 
}
 
//on combo box
 

private void comboBoxSurahName_SelectedIndexChanged(object sender, EventArgs e)
{
//if(comboBoxSurahName.SelectedIndex==114)
comboBoxSurahName.Items.RemoveAt(comboBoxSurahName.Items.Count - 1);
 

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100