Gianni Giordano
Gianni Giordano

Reputation: 143

Item in DropDownList

In a winform I have a DDL that is connected to a field in a MySQL database. In it there is a list of items that the user must select one, and then click OK to confirm and exit. Here's okay. However, if the user opens this form, I wish to appear in the DDL selected item previously. Will you give me a hand please? thanks

Com.CommandText = "SELECT ... FROM ... WHERE ..."; 

reader = Com.ExecuteReader(); 
while (reader.Read()) 
{
    testo1.Text= reader["richieste_1"].ToString(); // it's OK 
}  
//CmbCausa1.SelectedText = reader["causeID_1"].ToString(); // NOK  
//CmbCausa1.Items.Add(reader["causeID_1"].ToString()); NOK -->

Upvotes: 1

Views: 105

Answers (2)

Mikael Engver
Mikael Engver

Reputation: 4768

Try the following for your ComboBox:

var selected = reader["causeID_1"].ToString();
...
var index = CmbCausa1.FindString(selected);
CmbCausa1.SelectedIndex = index;

More about the FindString method can be found here: http://msdn.microsoft.com/en-us/library/wxyt1t12.aspx

Upvotes: 1

Yatrix
Yatrix

Reputation: 13795

If your application is an install, you can have a settings file in the install folder.

If it's not, you can save that value in the database, since the application uses a database already.

There is also the option of the applications settings, which will allow you to save that information. This option, I feel, is best.

Upvotes: 1

Related Questions