通过算法检查两打值、文本框与数据列

发布于 2024-07-14 06:39:19 字数 460 浏览 9 评论 0原文

好吧,各位大佬,这与其说是要求,不如说是挑战。 我有点难住了。 我通常只需要朝正确方向的刺棒,所以准备好你的刺棒。

我有一个被文本框覆盖的选项卡控件。 我想在同一表单的列表视图上的 SelectedIndexChanged 事件期间检查所有文本框的内容。 如果其中一个文本框的数据与 DataTable 行不同(由 ListView 项表示),我希望它询问用户是否愿意保留他们刚刚所做的更改。 如果没有任何改变,我希望它只改变选择。

显然,我正在将文本框的内容与数据行中的关联列进行比较。

我可以直接进行强力检查,一次对每个人进行一项检查。 我更愿意想出一些巧妙的算法方法来循环浏览选项卡控件文本框并根据柱状值检查值。

有什么建议么?

编辑:我最喜欢下面的“巧妙命名的文本框”解决方案,尽管两者都很好。 如果在接下来的 14 天内没有其他人有更好的想法,则文本框答案将变为绿色。

Okay big brains here's something that's more of a challenge than a requirement. I am a bit stumped. I usually just need a prod in the right direction, so get your prodding sticks ready.

I have a tabcontrol covered in textboxes. I want to perform a check of the contents of all the textboxes during the SelectedIndexChanged event on a listview on the same form. If one of the textboxes has data different from a DataTable row - represented by the ListView Item - I want it to ask if the user would like to keep the change they just made. If nothing has changed I want it to just change the selection.

So obviously I'm comparing the contents of the text boxes against associated columns in the datarow.

I could just brute force the check and do each individual check one at a time. I'd prefer to come up with some clever algorithmic way of cycling through the tabcontrol textboxes and checking the values against the columnar values.

Any suggestions?

EDIT: I like the "cleverly named textboxes" solution below best, although both are good. If no one else has a better idea in the next 14 days the textbox answer gets the green.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-07-21 06:39:20

为文本框指定一个巧妙的名称,因为名称的一部分是列/行名称。

将文本框控件分组为循环。 对于每个控件,获取(部分)名称并将其用作数据表的引用。 检查数值。

Give the textboxes a clever name as in a portion of the name is the column/row name.

Group the textbox controls an loop through them. For each control, get the (portion)name and use that as a reference to your datatable. Check the values.

滴情不沾 2024-07-21 06:39:20

如果我理解正确,您希望避免在每次更改时比较每个文本框,而只检查由 ListView 控件的 SelectedIndexChanged 事件驱动的已更改的文本框。 是对的吗?

那么,DataRows 和 DataTables 已经实现了行版本控制和回滚,因此,如果您将文本框绑定到基础行(通过编写事件来写回更改/失去焦点,或者使用自动化机制来完成相同的任务),然后检查 SelectedIndexChanged 上的 RowState 属性。 如果 RowState 未发生任何变化,则提示用户保存。 如果他保存,则提交更改,否则拒绝它们。

因此,例如,您希望在 SelectedIndexChanged 事件处理程序中使用类似的内容:

if (row.RowState == DataRowState.Modified) {
  // prompt for user input
  if (promptResult == PromptResult.Save) {
    row.AcceptChanges();
  }
  else {
    row.RejectChanges();
  }
}

If I'm understanding you right, you want to avoid comparing every textbox on every change, in favour of just checking the textboxes that are changed, driven by the SelectedIndexChanged event of the ListView control. Is that right?

Well, DataRows and DataTables already have row versioning and rollbacks implemented, so if you bind the text boxes to the underlying row (either by writing events to write back on change/lose focus or by using an automated mechanism to accomplish the same task), then check the RowState property on SelectedIndexChanged. If the RowState is anything other than unchanged, prompt the user to save. If he saves, commit the changes, otherwise reject them.

So, for example, you'd want something like this in your SelectedIndexChanged event handler:

if (row.RowState == DataRowState.Modified) {
  // prompt for user input
  if (promptResult == PromptResult.Save) {
    row.AcceptChanges();
  }
  else {
    row.RejectChanges();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文