Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET:删除Dropdownlist中的特定项_C#_Asp.net - Fatal编程技术网

C# ASP.NET:删除Dropdownlist中的特定项

C# ASP.NET:删除Dropdownlist中的特定项,c#,asp.net,C#,Asp.net,我正在做一个时间开始和时间结束的代码。所以我做的是手工编码 以下是我的gui示例: 它是HH:MM 小时的值从00到23,分钟的值从00到59 情景: 我的开始时间是8:00[8小时,00分钟],它还有一个自动回邮功能。因此,对于小时的结束时间,它不会显示00-07,对于小时,它将只显示数字8到23 这是我的密码: int _intDiff = _tmrStart - _tmrEnd; for (int x = 0; x <= _intDiff; x++) { DropDown

我正在做一个时间开始和时间结束的代码。所以我做的是手工编码

以下是我的gui示例:

它是HH:MM

小时的值从00到23,分钟的值从00到59

情景:

我的开始时间是8:00[8小时,00分钟],它还有一个自动回邮功能。因此,对于小时的结束时间,它不会显示00-07,对于小时,它将只显示数字8到23

这是我的密码:

int _intDiff = _tmrStart - _tmrEnd;

for (int x = 0; x <= _intDiff; x++)
{  
  DropDownList3.Items.Remove(DropDownList3.Items[x]);
}
int\u intDiff=\u tmrStart-\u tmrEnd;

对于(int x=0;x如果向前迭代,则在第一次迭代中删除第一个元素,即00。在第二次迭代中,删除第二个元素,即no 02,因为01已向上移动到第一个元素。您需要向后迭代,以便将元素从最后一个元素删除到第一个元素。请尝试:

for (int x = _intDiff -1; x > -1; x--)
{  
  DropDownList3.Items.Remove(DropDownList3.Items[x]);
}

您正在删除索引,而不是值。因此,当您循环并删除1,2,3,4,5,6,7时
您的值会更改其索引

e、 g.
idx,val
0,1
1,2
2,3
3,4
4,5
5,6
6,7

删除0
0,2
1,3
2,4

删除1
0,2 1,4
2,5
3,6

等等


最简单的解决方案:向后删除项:)

上面的代码假定索引和值始终相同,但一旦删除其中一个项,则不是这样。LINQ使这变得简单

//DropDownList3.Items.Remove(DropDownList3.Items[x]);

var item = DropDownList3.Items.Select(p=>p.value == userSelectedValue).first();
DropDownList3.Remove(item);
在上面的代码中,您需要做的就是获取他们选择的值并将其放入userSelectedValue参数中。

您可以使用

DropDownList3.Items.Remove(DropDownList3.Items.FindByValue("02"));

@本罗宾逊……谢谢你!