c# - 控制更改其他 UserControl 上的控制

标签 c# winforms

我有一个包含组合框/下拉列表的用户控件。该用户控件可多次使用并动态添加到面板中。当我更改一个用户控件上组合框的值时,它会更改其余的吗?有谁知道如何排序?

enter image description here

所以需要澄清一下。当我更改顶部用户控件(7002)的组合框中的值时,它会将第二个用户控件组合框的值更改为我选择的任何值。

谢谢!

添加控件的代码;

foreach (Common.UserDTO UDTO in BLL.User.GetAllUsers())
{
    Admin_UserControls.UserBar UB = new Admin_UserControls.UserBar(UDTO);
    UB.Location = new Point(0, int.Equals(pnlUserBlock.Controls.Count, 0) ? 0 : pnlUserBlock.Controls[pnlUserBlock.Controls.Count - 1].Bottom);

    pnlUserBlock.Controls.Add(UB);
}

构造函数/加载事件:

private Common.UserDTO UDTO;

public UserBar(Common.UserDTO UDTO)
{ 
    InitializeComponent();

    /* Store the passed in UserDTO */
    this.UDTO = UDTO;
}

private void UserBar_Load(object sender, EventArgs e)
{
    /* Setup the Drop down list */
    cbRanks.DataSource = Common.Helper.GetRanksDT();
    cbRanks.DisplayMember = "Rank";
    cbRanks.ValueMember = "ID";

    /* Setup the users */
    lblUsername.Text = UDTO.Username;
    cbRanks.SelectedValue = UDTO.RankID;
}

最佳答案

如果其他人将来遇到同样的问题,请将以上评论放入答案中。

创建的UserControl 的每个实例都绑定(bind)到同一个DataSet,从而提供此结果。

由线路引起:

cbRanks.DataSource = Common.Helper.GetRanksDT();

要解决此问题,只需在每次创建 UserControl 时声明一个新实例,请参阅 this post讨论了一些方法。

关于c# - 控制更改其他 UserControl 上的控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34594173/

相关文章:

C#、WinForms : What would prevent KeyDown events from chaining up from focused control up to main Form? 只有叶控件 KeyDown 对我有用

c# - HBITMAP *位图到 .net 位图

c# - 在处理表单时处理非组件?

c# - 使多行文本框容纳更多文本(C#、w​​inform)

c# - 在 WinForms 中动态获取面板中加载的控件详细信息

c# - 在 DevExpress PivotGridControl 中使用 DisplayNameAttribute

c# - 如何在时间上递增?

c# - 在 C# 8.0 中反序列化对象时处理临时空引用类型?

c# - 如何获取网络适配器的IP地址

c# - 读取 Excel 工作表时,如果列的值为数字,则数据表中返回 null