2016-10-11 35 views
-1

我有一個Telerik RadDropDownList顯示在database表中的城市,在後臺得到更新。每當數據庫更新時,我都想刷新RadDropDownList,但我找不到任何有用的信息。 我的代碼是在這裏:如何強制telerik radDropDownList顯示新添加到數據庫?

private void radDropDownList3_KeyUp(object sender, KeyEventArgs e) 
    { 
     _result = radDropDownList3.DropDownListElement.Text; 
     bool any = radDropDownList3.DropDownListElement.Items.Any(item => item.Text == _result.Trim()); 
     if (e.KeyCode == Keys.Enter && !any) 
     { 
      try 
      { 
       string city = ArToFa(_result); 
       using (var bas = new SaleToFreightageEntities()) 
       { 
        bas.Tariff.Add(new Tariff { City = city }); 
        bas.SaveChanges(); 
       } 
       MessageBox.Show(@"item added."); 
       tariffBindingSource.ResetBindings(true); 
       radDropDownList3.Rebind(); 
       radDropDownList3.Refresh(); 
       radDropDownList2.Enabled = false; 
       _result = ""; 
      } 
      catch (Exception ex) 
      { 
       if (ex.GetBaseException().Message.Contains("Cannot insert duplicate key row")) 
        MessageBox.Show(@"duplicated item!", @"", MessageBoxButtons.OK, 
         MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign); 
       _result = ""; 
      } 
     } 
    } 
+0

你是否嘗試使用計時器+ backgroundWorker問db,如果db更新更新datesource raddropdownlist? –

+0

不,我手動更新數據庫,並且希望在數據庫更新時,radDropDownList在數據庫末尾顯示最後插入的數據庫對象 – kavir

+0

如果您在後面的數據庫中更新,請重新綁定您的raddropdownlist。分享你的代碼。 –

回答

1

你必須在輸入數據後動態地在RadDropDownList中插入輸入的文本。您可以將RadListDataItem添加到您的RadDropDownList。 使用此代碼來實現此目的。這個項目將被添加到你的控制,所以你不需要重新綁定它,只有當你確定位置被添加到數據庫時才添加它。

RadListDataItem dataItem = new RadListDataItem(); 
dataItem.Text = "enteredText"; 
this.yourDropDownList.Items.Add(dataItem); 

你也可以通過更新後的DB方法再次得到數據源,並把它DROPDOWNLIST呼籲解決您的問題。

+0

你的代碼正在工作,但調用更新方法不起作用 – kavir

-1

你有很多方式 1。使一個timer.on你的數據庫使一個字段名稱_update(真假) 這個字段是真的,如果有你的數據庫添加或更新或刪除數據 並在此計時器檢查此字段。如果真正刷新並重新選擇數據,則不要執行任務 2。你可以做一個線程。這項工作太像定時器

你必須使字段bcoz如果定時器u刷新可能無法選擇數據,都想刷新。 如果你有問題告訴我

+0

我認爲一定是最簡單的方法來做到這一點,但我不知道如何。 – kavir