Click here to Skip to main content
14,266,079 members
Rate this:
Please Sign up or sign in to 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 ?

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
   
Do this arithmetic in data layer, not UI.
—SA

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try this:
 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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100