【问题标题】:how to bind data from a model to a text-box instead of a list-box如何将模型中的数据绑定到文本框而不是列表框
【发布时间】:2019-10-07 15:22:30
【问题描述】:

我通过 dapper 和数据模型从我的数据库中获取数据,然后将该数据绑定到列表框,一切正常,但现在我想将该数据绑定到文本框。以前我只是将数据绑定到一个隐藏的列表框,然后从列表框中获取该数据,但我现在想学习如何正确地做到这一点。

          //this is my current workaround.
        textBox1.Text = description.GetItemText(description.SelectedItem);

我也尝试将加载清单绑定到文本框,但这不起作用。

        //datamodel prop
        description.DataSource = loadinventory;

        //datamodel full prop
        description.DisplayMember = "ItemDescription";

       textBox1.DataBindings.Clear(); 
       textBox1.DataBindings.Add(new Binding("text", loadinventory, 
       "ItemDescription"));

这很完美。

        textBox1.DataBindings.Clear();
        textBox1.DataBindings.Add(new Binding("text", loadinventory, 
        "ItemDescription"));

我想使用数据模型和 dapper 从我的数据库中提取数据到我的文本框和其他变量。

【问题讨论】:

    标签: c# mysql data-modeling


    【解决方案1】:

    请尝试

    textBox1.DataBindings.Add("text", loadinventory, "ItemDescription");
    

    更多细节https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.control.databindings?view=netframework-4.8

    【讨论】:

    • + $exception {"这会导致集合中的两个绑定绑定到同一个属性。\r\n参数名称:绑定"} System.ArgumentException
    • 我得到了那个错误。我试图从文本框中清除绑定,然后运行给定的代码,但没有任何反应。
    • 我试过这个 textBox1.DataBindings.Add(new Binding("text", loadinventory, "ItemDescription"));
    • 不,一切运行良好,但盒子是空的。当我休息时,它表明实际上正在加载值,但现在我这只是与数据的显示有关
    • 请刷新文本框。
    猜你喜欢
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多