我将所有TextBox的标准数据绑定设置为对象,如下所示:
TextBoxMenuID.DataBindings.Add("Text", _selectedObject, "ID");
我想将一些TextBox绑定到List<>该对象中的索引如下:
TextBoxQ1.DataBindings.Add("Text", _selectedObject._qList[0].QuestionString, null);
绑定不是这样的。任何想法如何进行这种约束?
谢谢, SleffTheRed
答案 0 :(得分:4)
如果您使用通用列表作为数据集合,那么您可以这样做:
List<Person> list;
public Form1()
{
InitializeComponent();
list = new List<Person>();
list.Add(new Person { ID = 1, Name = "Name 1", Age = 21 });
list.Add(new Person { ID = 2, Name = "Name 2", Age = 28 });
textBox1.DataBindings.Add(new Binding("Text", list, "ID", false));
textBox2.DataBindings.Add(new Binding("Text", list, "Name", false));
textBox3.DataBindings.Add(new Binding("Text", list, "Age", false));
}
internal class Person
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
答案 1 :(得分:2)
感谢Mitja的代码,但要更改索引只需添加[]的:
list = new List<Person>();
list.Add(new Person { ID = 1, Name = "Name 1", Age = 21 });
list.Add(new Person { ID = 2, Name = "Name 2", Age = 28 });
list.Add(new Person { ID = 3, Name = "Name 3", Age = 44 });
textBox1.DataBindings.Add(new Binding("Text", list[0], "Name", false));
textBox2.DataBindings.Add(new Binding("Text", list[1], "Name", false));
textBox3.DataBindings.Add(new Binding("Text", list[2], "Name", false));
internal class Person
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}