【问题标题】:How to sum one column in gridview and show it in a textbox如何对gridview中的一列求和并将其显示在文本框中
【发布时间】:2016-11-16 22:25:40
【问题描述】:
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As        System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("mcbdatabseConnectionString").ConnectionString
        Using con As New MySqlConnection(constr)
            Using cmd As New MySqlCommand("SELECT * FROM mcbdatabse.subject_enrolled")
                Using sda As New MySqlDataAdapter()
                    cmd.Connection = con
                    sda.SelectCommand = cmd
                    Using dt As New DataTable()
                        sda.Fill(dt)
                        GridView1.DataSource = dt
                        GridView1.DataBind()
                    End Using
                End Using
            End Using
        End Using
     End If
End Sub

我的 gridview 有此代码,我想汇总所有单位并将其显示在我的文本框上;txtTotalUnits 谢谢。This is my gridview and textbox

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:

    您可以为 GridView.RowDataBound Event 添加事件处理程序,如下所示

    Dim total as Integer
    
    Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles CustomersGridView.RowDataBound
    
        If e.Row.RowType = DataControlRowType.DataRow Then
    
          total = total + Convert.ToInt32(e.Row.Cells(the_column_index).Text) .
          txtTotalUnits.Text = Convert.ToString(total)
    
        End If
    
      End Sub
    

    MSDN上的更多类似示例

    【讨论】:

    • 我有一个错误,字符串类型的值不能是'System.Web.UI.WebControls.Textbox
    • 我修复了错误,但它没有在文本框上显示任何结果?
    • 您实际上是否将该方法作为事件处理程序添加到您的 GridView.RowDataBound 事件中?我刚刚编辑了我的答案,并在方法的声明中添加了一个 Handles 子句。用您的 GridView 名称替换 GridView 的名称,然后重试
    • Gab you are wrecome:快乐编码
    【解决方案2】:

    使用数据源表,需要using System.Linq;

    var result = tbl.AsEnumerable().
                     Sum(x => Convert.ToDecimal(x["ColumnNameForWhichYouWantSum"].ToString()));
    
    txtTotalUnits.Text = result.ToString(); 
    

    【讨论】:

    • 什么是 tbl.AsEnumerable()?
    • 我不知道如何将此代码转换为 vb.net 代码?
    • @Gab 检查此 linq 以了解 VB.NET 和 C# 实现。 msdn.microsoft.com/en-us/library/…
    【解决方案3】:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("mcbdatabseConnectionString").ConnectionString
        Using con As New MySqlConnection(constr)
          Using cmd As New MySqlCommand("SELECT * FROM mcbdatabse.subject_enrolled")
            Using sda As New MySqlDataAdapter()
              cmd.Connection = con
              sda.SelectCommand = cmd                    
              Using dt As New DataTable()
                sda.Fill(dt)
                Dim total As Integer = 0
                For i As Integer = 0 To dt.Rows.Count - 1
                  Dim drw As DataRow = dt.Rows(i)
                  Dim units As Integer = CInt(drw("Units")) 'Assuming Units is the column name'
                  total += units
                Next i
                txtTotalUnits.Text = total.ToString
                GridView1.DataSource = dt
                GridView1.DataBind()       
              End Using
            End Using
          End Using
        End Using
      End If
    End Sub
    

    【讨论】:

    • 但是在 asp.net 中你不能使用 DataGridView1.Rows。我可以导入一些东西来启用它吗?
    • 糟糕,抱歉。我错过了这是 ASP.NET
    • 对不起,我是一只鹅,只是总结一下数据表。我已经编辑了我的答案
    • 我应该先声明这个使用 sda 作为新的 MySqlDataAdapter() 吗?导致我遇到错误“未声明 sda”我在按钮上使用此代码?谢谢
    • 抱歉,我的意思是您应该替换原始代码中的 Using dt as New DataTable 块。我已经编辑显示整个代码块
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多