4 Replies - 5807 Views - Last Post: 13 October 2012 - 05:21 AM Rate Topic: -----

#1 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

sum datagridview rows (data-bound) with result inside it.

Posted 13 October 2012 - 12:29 AM

hi all,

i have a DGV with 2 columns (data-bound/data from database), column1 is product_name, column2 is price, i have to calculate/sum column2 (price) and give the result in DGV it self (last row of column2), when i use this code :

        Using conn As New MySqlConnection("connectionString")
            conn.Open()
            Dim command As New MySqlCommand("select product_name, price from product", 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 total As Double
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                total = total + DataGridView1.Rows(i).Cells(1).Value
            Next
            DataGridView1.Rows.Add(total)
        End Using



it give me an error : "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

any idea how to solve this?

thanks.

This post has been edited by artemix22: 13 October 2012 - 12:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: sum datagridview rows (data-bound) with result inside it.

#2 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: sum datagridview rows (data-bound) with result inside it.

Posted 13 October 2012 - 12:53 AM

never mind, already solve.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: sum datagridview rows (data-bound) with result inside it.

Posted 13 October 2012 - 04:04 AM

Wanna post your solution so that if others find this topic in a search, they can see what you did to accomplish this?
Was This Post Helpful? 0
  • +
  • -

#4 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: sum datagridview rows (data-bound) with result inside it.

Posted 13 October 2012 - 04:31 AM

View PostCharlieMay, on 13 October 2012 - 04:04 AM, said:

Wanna post your solution so that if others find this topic in a search, they can see what you did to accomplish this?


ok, i simply add new row for DataTable, here is the code :

       Using conn As New MySqlConnection("connectionString")
            conn.Open()
            Dim command As New MySqlCommand("select product_name, price from product", 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 total As Double
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                total = total + DataGridView1.Rows(i).Cells(1).Value
            Next
            Dim myrow = dt.NewRow
            myrow(0) = "Amount"
            myrow(1) = total
            dt.Rows.Add(myrow)
        End Using



hope it helps.
Was This Post Helpful? 1
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: sum datagridview rows (data-bound) with result inside it.

Posted 13 October 2012 - 05:21 AM

Thanks for sharing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1