Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have few tables with same column name in ms access database . I am loading tables in datagridview selected from combobox.I want to display sum of a column in text box /level. . I have found some code that works with datagridviews column created in design time. can some one provide me with a link or sample code ?

VB
Private adapter As New SqlDataAdapter(String.Empty, Me.con)
  Private data As DataTable
  Private Sub Form1_Load(ByVal sender As Object, _
                     ByVal e As EventArgs)
      con.Open()
      Me.ComboBox1.DisplayMember = "TABLE_NAME"
      Me.ComboBox1.ValueMember = "TABLE_NAME"
      Me.ComboBox1.DataSource = Me.con.GetSchema("TABLES", New String() {Nothing, Nothing, Nothing, "TABLE"})

      con.Close()
  End Sub
  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
                                         ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
      If Me.ComboBox1.SelectedItem IsNot Nothing Then
          Me.data = New DataTable
          Me.adapter.SelectCommand.CommandText = String.Format("SELECT * FROM [{0}]", Me.ComboBox1.SelectedValue)
         Me.adapter.Fill(data)
         Me.DataGridView1.DataSource = Nothing
         Me.DataGridView1.Columns.Clear()
         Me.DataGridView1.DataSource = Me.data
      End If
Posted
Comments
Sergey Alexandrovich Kryukov 22-Jan-16 22:58pm     CRLF
Do this arithmetic in data layer, not UI. —SA

1 solution

Try this:
VB.NET
 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim WantedValue As Integer = 0
For Each dr As DataGridViewRow In DataGridView1.Rows
    WantedValue = WantedValue + CInt(dr.("AccessTableColumnNameYouWant").value.tostring)
Next
TextBox1.Text = WantedValue.ToString
End sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900