DOODpls DOODpls - 3 months ago 12
C# Question

Add "---OTHERS---" on top of mysql-populated combobox

I am having a hard time figuring this out.. I want to add a

"---OTHERS---"
text on top of my
ComboBox
, but my
ComboBox
is populated with MySQL:

conDatabase = new MySqlConnection(constring);
MySqlCommand cmdDatabase = new MySqlCommand("select distinct item_type from Company_db.inventory", conDatabase);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDatabase;
DataSet dbdataset = new DataSet();
sda.Fill(dbdataset);
cbPart.DataSource = dbdataset.Tables[0];
cbPart.DisplayMember = "item_type";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Answer

You need to add the extra item to the rows collection of the datatable before binding it to the datasource.

....
sda.Fill(dbdataset);

DataTable dt = dbdataset.Tables[0];
DataRow row = dt.NewRow();
row.ItemArray = new object[] {"--OTHERS--"};
dt.Rows.InsertAt(row, 0);

cbPart.DataSource = dt;
Comments