Gab Gab - 1 year ago 64 Question

How to sum one column in gridview and show it in a textbox

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()
GridView1.DataSource = dt
End Using
End Using
End Using
End Using
End If
End Sub

I have this code for my gridview, I want to total all the units and show it on my textbox;txtTotalUnits Thanks.This is my gridview and textbox

Answer Source

You could add an event handler for the GridView.RowDataBound Event as follows

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

More similar examples on MSDN

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download