调用 tableAdapter 的 Fill() 方法时如何防止 selectedValue 更改?

发布于 2024-07-11 01:27:50 字数 354 浏览 5 评论 0原文

我已将列表框绑定到一些数据。

问题是当我调用 myTableAdapter.Fill(..) 方法时,SelectedValue 更改为列表中第一个项目 ID。 虽然VS中的“选定值”没有绑定到任何地方(见图)。 替代文本 http://img370.imageshack.us/img370/2548/ss20090108212745qz2.png< /a>

请问我该如何防止这种行为?

非常感谢您的帮助。

I have bound my ListBox to some data.

The problem is when I call myTableAdapter.Fill(..) method, SelectedValue changes to whatever is the first item ID in the list. Although "Selected Value" in VS is not bound anywhere (see image).
alt text http://img370.imageshack.us/img370/2548/ss20090108212745qz2.png

How do I prevent this behaviour, please?

Thank you very much for helping.

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

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

发布评论

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

评论(2

等你爱我 2024-07-18 01:27:50

您不应该对每个请求都具有约束力。 如果由于某种原因您绝对必须绑定每个请求,则必须手动在 ListBox 上设置 SelectedIndex。 这是因为 Fill 方法首先清除列表,然后为获取的数据创建新的列表项。

You shouldn't be binding on each request. If you absolutely have to bind on each request for some reason you have to set SelectedIndex on the ListBox manually. This is because the Fill method first clears the list then creates new list items for the fetched data.

会发光的星星闪亮亮i 2024-07-18 01:27:50

我能想到的最简单的方法是将表适配器填充代码更改为如下所示:

string preSelected = myDropDownList.SelectedValue;
myTableAdapter.Fill(myDataTable);
myDropDownList.SelectedValue = preSelected;

如果该项目不再存在,您将遇到问题,因此您可能需要添加一个条件来检查该情况。

The simplest way I can think of is to change your table adapter fill code to something like this:

string preSelected = myDropDownList.SelectedValue;
myTableAdapter.Fill(myDataTable);
myDropDownList.SelectedValue = preSelected;

You will run into an issue if the item doesn't exist anymore, so you may want to add a condition to check for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文