【问题标题】:Dynamic gridview column value dependent on another column value动态gridview列值依赖于另一个列值
【发布时间】:2014-02-13 22:25:08
【问题描述】:

我有一个包含三个绑定字段的 gridview。 “日期”,即添加支持票的日期; “固定日期”,即解决支持票的日期;和“等待天数”,它应该是未解决票证的天数。

每一行都有不同的日期,可以有也可以没有 dateFixed。我可以使用 RowDataBound 事件从 date 和 dateFixed 计算 daysWaited,但我不知道如何将 daysWaited 设置为每一行的计算值。

我正在使用 VB.NET 和 .NET 4.0 框架。

【问题讨论】:

  • 这是在 VB.Net 还是 ASP.Net 中完成的?
  • 你不能让它成为你查询的一部分来获取数据吗?
  • @MrCoDeXer:是ASP.NET,在后面的代码中使用了VB.NET。
  • 我给了你一个可行的方法,但如果你能把它作为你查询的一部分,就像@GuyNethery 提到的那样会好得多。

标签: asp.net .net vb.net gridview


【解决方案1】:

不确定您如何加载数据,希望设置gridview的数据源。设置好数据源后,就可以调用这个方法了。它将遍历并比较每一行的日期,它还会检查是否有任何内容。这在我的测试期间效果很好。希望您发现这对您的使用有好处。

 Private Sub CalcDays()
    For i As Integer = 0 To GridView1.Rows.Count - 1
        Dim stDate As Date
        Dim endDate As Date
        Dim daysPassed As Integer = -1

        If Not (GridView1.Rows(i).Cells(1).Text = " ") Then
            stDate = DateTime.Parse(GridView1.Rows(i).Cells(0).Text)
            endDate = DateTime.Parse(GridView1.Rows(i).Cells(1).Text)

            While (stDate <= endDate)
                stDate = stDate.AddDays(1)
                daysPassed += 1
            End While

            GridView1.Rows(i).Cells(2).Text = daysPassed.ToString
        Else
            GridView1.Rows(i).Cells(2).Text = "NA"
        End If
    Next
 End Sub

这是我的测试截图...

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多