i have a datagridview with 2 columns on it.

i add column 1 manually from Datagridview task (add columns), it should numeric value and came from user input.

for column 2 (it's numeric value), on load event from database with this :

Using conn As New MySqlConnection("connection string")
                conn.Open()
                Dim command As New MySqlCommand("select amount from my_tabel_in_database", conn)
                Dim adapter As New MySqlDataAdapter
                Dim dt As New DataTable
                adapter.SelectCommand = command
                adapter.Fill(dt)
                DataGridView1.DataSource = dt
                adapter.Dispose()
                command.Dispose()
                conn.Close()


   Dim total1, total2 As Double
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                total1 += DataGridView1.Rows(i).Cells(0).Value
                total2 += DataGridView1.Rows(i).Cells(1).Value
            Next
            Dim myrow = dt.NewRow
            myrow(0) = total1
            myrow(1) = total2
            dt.Rows.Add()
            dt.Rows.Add(myrow)
End Using

my purpose is to add total1 and total2 in bottom of that both column, but i get this error :

Cannot find Column 1

how to insert that both total in the bottom of column?

thanks.

sorry, code above is wrong, here is the code :

Using conn As New MySqlConnection("my connection string")
            conn.Open()
            Dim command As New MySqlCommand("select amount from test", conn)
            Dim adapter As New MySqlDataAdapter
            Dim dt As New DataTable
            adapter.SelectCommand = command
            adapter.Fill(dt)
            DataGridView2.DataSource = dt
            adapter.Dispose()
            command.Dispose()
            conn.Close()

            Dim total1, total2 As Double

            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                DataGridView2.Rows(i).Cells(0).Value = DataGridView2.Rows(i).Cells(1).Value * 2
                total1 += DataGridView2.Rows(i).Cells(0).Value
                total2 += DataGridView2.Rows(i).Cells(1).Value
            Next

            Dim myrow = dt.NewRow
            myrow(0) = total1
            myrow(1) = total2 ---->> error here
            dt.Rows.Add()
            dt.Rows.Add(myrow)
End Using

try following this pattern instead,

   Dim dt As New DataTable
   Dim r As DataRow
   '*************************
   ' simulate Database Load
   With dt
      .Columns.Add("FromDataBase", GetType(Double))
      r = .NewRow : r(0) = 5 : .Rows.Add(r)
      r = .NewRow : r(0) = 15 : .Rows.Add(r)
      r = .NewRow : r(0) = 25 : .Rows.Add(r)
      r = .NewRow : r(0) = 55 : .Rows.Add(r)
   End With
   '*************************

   'add your new column to the datatable not the DGV
   Dim col1 As New DataColumn("Column1", GetType(Double))
   dt.Columns.Add(col1)
   col1.SetOrdinal(0) ' make it the first column
   DataGridView1.DataSource = dt

   ' sum columns
   r = dt.NewRow
   r(0) = dt.Compute("Sum([Column1])", filter:="True")
   r(1) = dt.Compute("Sum([FromDataBase])", filter:="True")
   dt.Rows.Add(r)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.