Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 too;-)
Posted

1 solution

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.
 
Share this answer
 
Comments
Muhamad Faizan Khan 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900