Stefano Schivari
Stefano Schivari

Reputation: 57

Refresh dropdownlist populated from datasource c#

Hi I'm developing a smart WM 6.1 app using CF 3.5 and visual studio 2008.

So i have a dropdown list populated via datasource (a table of my sqlite database).

My question is how can i refresh (and fill it with the new data) this dropdown list without closing and re open the form?

My idea is to refresh it after clicking a button on the form.

i try with :

 List.Update();
 List.Refresh();

 List.BeginUpdate();
 List.DataSource() = Data;
 List.EndUpdate();

Thanks

Upvotes: 1

Views: 2921

Answers (1)

user153923
user153923

Reputation:

First, get your data.

DataTable table = new DataTable();
table.Load(sqliteCmd.ExecuteReader());

If that is successful, continue.

if (0 < table.Rows.Count) {
}

If you want to be fancy, save the currently selected item so you can reselect it after you update your DropDownList control.

  string last = ddlCtrl.Items[ddlCtrl.SelectedIndex].ToString();

Clear the items from your DropDownList control.

  ddlCtrl.Items.Clear();

Add the info from your data to your DropDownList control.

  foreach (DataRow r in table.Rows) {
    ddlCtrl.Items.Add(r[0].ToString());
  }

If you were being fancy, reselect that last item.

  if (!String.IsNullOrEmpty(last)) {
    for (int index = 0; index < ddlCtrl.Items.Count; index++) {
      if (ddlCtrl.Items[index].ToString() == last) {
        ddlCtrl.SelectedIndex = index;
        break;
      }
    }
  }

Upvotes: 1

Related Questions