【问题标题】:How to force telerik radDropDownList to show newly added to database?如何强制telerik radDropDownList 显示新添加到数据库?
【发布时间】:2016-10-11 07:42:31
【问题描述】:

我有一个Telerik RadDropDownList 来显示database 中表格中的城市,该表格在后台更新。我想在数据库更新时刷新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 = "";
            }
        }
    }

【问题讨论】:

  • 您是否尝试使用 timer+backgroundWorker 询问 db 以及 db 是否更改更新 datesource raddropdownlist?
  • 不,我手动更新数据库,我希望在更新数据库时,radDropDownList 在其末尾显示最后插入到数据库的对象
  • 如果您在后台数据库中更新之后重新绑定您的 raddropdownlist。分享您的代码。
  • 让我解释一下,我有一个带有数据源绑定的“radDropDownList”,当我在列表中搜索所需的项目时,如果列表不包含该键入的单词,我就是按下“Enter”,在“radDropDownList3_KeyUp”事件中,它将被添加到数据库中,但我希望当它添加到数据库时,“radDropDownList”也显示新添加的项目,

标签: c# winforms telerik


【解决方案1】:

在插入数据库后,您必须在RadDropDownList 中动态插入此输入文本。您可以将RadListDataItem 添加到您的RadDropDownList。 使用此代码来实现这一点。该项目将被添加到您的控件中,因此您不需要重新绑定它,只有在您确定该位置已添加到 db 时才添加它。

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

您还可以通过在更新数据库方法之后调用再次获取数据源并将其放入下拉列表来解决您的问题。

【讨论】:

  • 您的代码正在运行,但调用更新方法不起作用
【解决方案2】:

你有很多方法 1.做一个计时器。在你的数据库上做一个字段来命名 _update(true False) 如果您的数据库添加或更新或删除数据,此字段为真 并在此计时器上检查此字段。如果真正刷新并重新选择数据,否则不执行任务 2.你能做一个线程吗?这个工作也像计时器

如果你在计时器上刷新,你必须使字段 bcoz 可能无法选择数据并且都想刷新。 如果你有问题告诉我

【讨论】:

  • 我认为必须是最简单的方法,但我不知道如何。
猜你喜欢
  • 2019-03-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多