从 WebDataGrid 填充 DropDownList

发布于 2024-10-07 14:48:13 字数 436 浏览 0 评论 0原文

我有一个链接到 SqlDataSource 的 WebDataGrid 来显示数据。选择一行后,我想使用该行中的数据填充一些文本框和下拉列表,以进行编辑。文本框工作正常;我使用以下代码选择网格中的第二个字段(日期)并填充相应的文本框(我使用的是 VB):

txtDate.Text = currentRow.Items(2).Value

但是,当我尝试使用相同的代码结构填充下拉列表时,出现以下错误:

'ddlType' has a SelectedValue which is invalid because it does not exist in the list of items.

我认为问题可能是下拉列表是使用 type_id 填充的,而网格显示 type_name 。这会导致问题吗?有没有办法解决这个问题?

I have a WebDataGrid linked to a SqlDataSource to display data. When a row is selected, I want to populate some textboxes and dropdownlists with the data from that row, for editing purposes. The textboxes work fine; I use the following code to select the second field in the grid (Date) and populate the corresponding textbox (I'm using VB):

txtDate.Text = currentRow.Items(2).Value

However, when I try to populate the dropdownlists using the same code structure, I get the following error:

'ddlType' has a SelectedValue which is invalid because it does not exist in the list of items.

I think the problem may be that the dropdownlists are populated using type_id, while the grid displays type_name. Would this be causing the problem, and is there a way to get around this?

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

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

发布评论

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

评论(2

失与倦" 2024-10-14 14:48:13

您可以尝试以下几种操作:

  1. 更改 SQLDataSource 以同时返回 type_id 并将该列设置为visible="false",这样您就可以选择该字段而不是名称并将其用于 .SelectedValue。

  2. 如果这是不可能的,那么您可以使用这样的代码(只有当下拉列表中显示的文本是唯一的并且与网格视图中显示的文本完全相同时,这才有效):

    '将其替换为对 currentRow.Items(#).Value 的调用
    将某些文本变暗为字符串=“type_name”

    DropDownListName.Items.FindByText(sometext).Selected = True

  3. 另一种可能性是循环遍历下拉列表中的每个元素并找到文本,这就像暴力方法但有效。

    '将其替换为对 currentRow.Items(#).Value 的调用

    将某些文本调暗为字符串 =“type_name”

    对于 ddArriveAMPM.Items 中的每个 ddItem 作为 ListItem

     `如果 String.Compare(sometext, ddItem.Text, True) = 1 那么`   
         `ddItem.Selected = True`
      `结束如果`        
    `下一步`
    

There are couple of things you could try:

  1. Change the SQLDataSource to return the type_id also and set the column to visible="false" that way you could select that field instead of the name and use that for .SelectedValue.

  2. If that is not possible then you could use code like this (this will only work if the text that is displayed in the drop down is unique and is exactly the same as what is displayed in the grid view):

    'replace this with the call to currentRow.Items(#).Value
    Dim sometext As String = "type_name"

    DropDownListName.Items.FindByText(sometext).Selected = True

  3. The other possibility is to loop through each element in the drop down and find the text, this is like the brute force method but works.

    'replace this with the call to currentRow.Items(#).Value

    Dim sometext As String = "type_name"

    For Each ddItem As ListItem In ddArriveAMPM.Items

     `If String.Compare(sometext, ddItem.Text, True) = 1 Then`   
         `ddItem.Selected = True`
      `End If`        
    `Next`
    
纸伞微斜 2024-10-14 14:48:13

您必须先将该项目添加到列表框中,然后才能选择它。

DropDownListX.Items.Add("Item");

DropDownListX.Items.Add(new ListItem("String","Value"));

确保在选择新行时清除下拉框,否则旧行值将保留在其中。

DropDownListX.Items.Clear();

之后您可以使用 SelectedValue

Cheers,
斯特凡

You have to add the item to the listbox before you can select it.

DropDownListX.Items.Add("Item");

or

DropDownListX.Items.Add(new ListItem("String","Value"));

Make sure to clear the DropDown box when selecting a new row otherwise the old rows values will stay in there.

DropDownListX.Items.Clear();

After this you can use SelectedValue

Cheers,
Stefan

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