简体   繁体   English

ASP.NET如何检查gridview中的每行?

[英]ASP.NET How do I check ever row in the gridview?

I want to add value into a Grid view via dropdownlist with a button. 我想通过带有按钮的dropdownlist将值添加到Grid视图中。 I want the ddTN.SelectedItem.value in the Grid view to be unique. 我希望“网格”视图中的ddTN.SelectedItem.value是唯一的。 No duplication How do I check every row for the ddTN.SelectedItem.value before adding a new ddTN.SelectedItem.value into the Grid view? 无重复如何在将新的ddTN.SelectedItem.value添加到“网格”视图之前,如何检查ddTN.SelectedItem.value的每一行?

This are the codes that I have and it keep comparing the value with the first value in the gridview. 这是我拥有的代码,它会不断将值与gridview中的第一个值进行比较。 Not the others. 没有其他人。

I don't want to use a checkbox and such. 我不想使用复选框等。 All the example I found required using checkbox. 我发现所有需要使用复选框的示例。

protected void Insert(object sender, EventArgs e)
{
   int i  = 0;
   var p  = 1;
   DataControlFieldCell cell = GridView1.Rows[i].Cells[p] as DataControlFieldCell;
   if (cell.Text != ddTN.SelectedItem.Value)
   {
      dt.Rows.Add(ddTN.SelectedValue, ddDuration.SelectedValue);
      ViewState["Customers"] = dt;
      this.BindGrid();
      label.Text = "";
      p++;
   }
   else
   {
      label.Text = "Exercise already inserted";
   }
}

It looks like you were intending on looping over the items in the grid but you have forgotten the looping mechanism. 看起来您打算循环遍历网格中的项目,但您忘记了循环机制。 In your code, it always only checks the first item because i is initialized to 0 and never changes. 在您的代码中,它始终仅检查第一项,因为i初始化为0且永不更改。

Try using a looping mechanism like a for loop or a while loop. 尝试使用诸如for循环或while循环之类的循环机制。 Or, if you know the items in the grid from the beginning, perhaps use a hash table for quickly checking if the selected item already exists. 或者,如果您从一开始就知道网格中的项目,则可以使用哈希表快速检查所选项目是否已经存在。

Keep trying, you are almost there! 继续努力,您快要准备好了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM