我正在使用visual studio 2010中的datarepeater控件10.0版。当我使用鼠标移动到下一行时,CurrentItemIndex似乎在文本框离开事件之前更新。因此,当我从文本框中检索值时,我现在不知道与之关联的ItemIndex。使用键盘移动到下一行时不会发生这种情况。有人看到这种情况发生datarepeater上的9.0版确实以这种方式工作。
答案 0 :(得分:0)
直接回答你,这里有一些C#片段。我假设您已经在问题中隐含了TextBox对象(或其他控件)。我还假设你在一个事件处理程序(例如TextChanged)中。如果你还没有这样做,你需要使用object sender参数而不是设计时声明的TextBox控件(即不要使用TextBox1或类似对象)因为它只是引用DataRepeaterItem模板控件而不是对您感兴趣的数据行的单独控制。
TextBox itemTextBox = sender as TextBox;
//* DataRepeaterItem is a control which contains other controls for each data "row"
DataRepeaterItem drItem = itemTextBox.Parent as DataRepeaterItem;
//* Retrieve the particular data item
int idx = drItem.ItemIndex;
//* If DataRepeater is bound to a BindingSource, for example,
//* one can retrieve the underlying data item
object dataItem = myBindingSource.List[idx];
我通过DataRepeater遇到了控制焦点和数据更新的各种错误和挑战。无法保证事件触发的确切顺序:Leave,LostFocus,CurrentItemChanged等。正如您所观察到的,它取决于您是在DataRepeater中使用鼠标还是从表单上的其他控件使用鼠标或使用键盘。可能确实存在已建立的算法,但我观察到与文档的差异。当数据处理框架(例如BindingSource,CurrencyManager)也订阅这些事件并根据您可能期望或想要的顺序更新事物时,情况就很复杂。我没有关于如何处理这些问题的建议,但我希望上面的代码至少可以让您访问您所在控件的特定索引和数据。