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
答案 0 :(得分:1)
答案 1 :(得分:0)
使用数据源表,您需要using System.Linq;
var result = tbl.AsEnumerable().
Sum(x => Convert.ToDecimal(x["ColumnNameForWhichYouWantSum"].ToString()));
txtTotalUnits.Text = result.ToString();
答案 2 :(得分:0)
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