【问题标题】:How to compare row values of two gridviews with dropdownlist column in asp.net?如何将两个gridviews的行值与asp.net中的下拉列表列进行比较?
【发布时间】:2014-06-04 05:46:14
【问题描述】:

我有两个网格视图。第一个有一个下拉列表。当用户单击名为“显示”的按钮时,数据将显示在数据来自数据库的两个网格视图上。具有下拉列表的列上的行数据必须与第二个网格视图的第一列上的行进行比较。如果它们相等,会提示一个消息框,说没有更改并且数据不会被保存,否则如果它们不相等,将显示模态弹出窗口询问数据是否正确。

下面是我的比较代码,但它只读取 gridview1 中第一行的值。

 For i = 0 To GridView1.Rows.Count - 1

            Dim ddl As DropDownList = DirectCast(GridView1.Rows(i).Cells(6).FindControl("dropdowncriteria"), DropDownList)
            Dim txt As TextBox = DirectCast(GridView1.Rows(i).Cells(7).FindControl("txtreason"), TextBox)


            If ddl.SelectedValue = GridView2.Rows(i).Cells(0).Text And txt.Text = GridView2.Rows(i).Cells(1).Text Then

                MessageBox("No Changes Made! Nothing will be Saved.")
                Return

            Else

                lblmsg.Text = "Are you sure that all the Data you've Selected/Entered are Correct?"
                mdlpopupmsg.Show()
                Return

            End If

  Next

这一定是什么问题?

提前致谢。

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:

    它只读取第一个值 (i=0),因为 return 语句会导致 for 循环在第一次比较后退出。如果要比较所有行,则需要一个变量来跟踪每行的 if 测试结果。像这样的:

        Dim hasChanges As Boolean = False
        For i = 0 To GridView1.Rows.Count - 1
            ...
            If ddl.SelectedValue = GridView2.Rows(i).Cells(0).Text And txt.Text = GridView2.Rows(i).Cells(1).Text Then
                'do nothing
            Else
                hasChanges = True
            End If
        Next
        If hasChanges Then
            MessageBox("Has changes.")
        Else
            MessageBox("No changes.")
        End If
    

    【讨论】:

    • 先生,我遇到了一个小问题......每当我删除条件'And txt.Text = GridView2.Rows(i).Cells(1).Text'时,它都能完美运行......但是当我添加了它,它没有..
    • 这取决于您要在应用程序中实现的具体目标。一个好的起点是在该行上放置一个断点并运行程序。然后,当断点被击中时,将鼠标悬停在 txt.Text 和 GridView2.Rows(i).Cells(1).Text 上以查看值,检查值是否符合您的预期?
    • 我按照你的建议做了,先生,它表明条件'txt.Text'没有返回任何东西......
    • 我在 Gridview 上查找文本框控件的代码是否正确?这个 'Dim txt As TextBox = DirectCast(GridView1.Rows(i).Cells(7).FindControl("txtreason"), TextBox)'
    【解决方案2】:
    Dim itemt As Double 
    If (DataGridCart.RowCount() > 0) Then
        For i = 0 To DataGridCart.Rows.Count - 1
            'if itemt as double
            itemt = Val(Trim(txtItem.Text))
    
            If ((DataGridCart.Rows(i).Cells("Item").Value).Equals(itemt)) Then
                        MsgBox("existing entry")
            End If
    
       Next
    
     End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      相关资源
      最近更新 更多