6 Replies - 7033 Views - Last Post: 26 January 2013 - 07:28 PM Rate Topic: -----

#1 artemix22  Icon User is offline

  • D.I.C Head

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

Datagridview Insert Row

Posted 24 January 2013 - 03:37 AM

i have a form with a datagridview on it, in that datagridview i have 2 columns, column1 i add manually through its task/properties and column2 i populate from database (MySQL).

i have a formula for column1 :

For i As Integer = 0 To Datagridview1.Rows.Count - 1
      Datagridview1.Rows(i).Cells(0).Value = Datagridview1.Rows(i).Cells(1) * 2
Next



i want to make Total for that both column, so i add this code on "For" looping above :

Dim total1, total2 As Double

For i As Integer = 0 To Datagridview1.Rows.Count - 1
      Datagridview1.Rows(i).Cells(0).Value = Datagridview1.Rows(i).Cells(1) * 2

      total1 += Datagridview1.Rows(i).Cells(0).Value
      total2 += Datagridview1.Rows(i).Cells(1).Value     
Next



i want to add that both total in the last Datagridview row, the problem is that Datagridview1 has bound and un-bound columns.

i have try this so far :

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


and i get "Cannot find column1" error.

PS :

here is the complete 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)
            DataGridView1.DataSource = dt
            adapter.Dispose()
            command.Dispose()
            conn.Close()

            Dim total1, total2 As Double

            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                DataGridView1.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Cells(1).Value * 2
                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


thanks for your help/reply.

This post has been edited by artemix22: 25 January 2013 - 12:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview Insert Row

#2 artemix22  Icon User is offline

  • D.I.C Head

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

Re: Datagridview Insert Row

Posted 25 January 2013 - 12:16 AM

can anyone help me please :helpsmilie:
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: Datagridview Insert Row

Posted 25 January 2013 - 07:33 AM

Where is the code for adding your unbound column to the datagrid? And why are you not adding the column to the datatable?

How are you populating it?

Please answer these questions with your appropriate code examples.


I see more problems, but let's first get these questions answered.
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: Datagridview Insert Row

Posted 25 January 2013 - 08:22 AM

Where is the code for adding your unbound column to the datagrid? = i'm not using any code, i add it manually through its task/properties by select my dgv than click little arrow in top right corner >> Add Column..

why are you not adding the column to the datatable? = i use datatable to bind data from database (amount column in my database) than make it source for my dgv, after that, i use that amount to run this formula :

DataGridView1.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Cells(1).Value * 2


Was This Post Helpful? 0
  • +
  • -

#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: Datagridview Insert Row

Posted 25 January 2013 - 08:31 AM

OK so I assume you're erroring on myrow(1) = total 2. Is that correct?

Because your datatable only has a single column, index 0 you cannot access index 1 inside the datarow.

The options you have at this point would be

1. Include the other column inside your datatable so that myRow(1) is valid or
2. Access that column with the standard
DataGridView1.Rows(index).Cells(index).Value = format (after the new datarow has been added)

This post has been edited by CharlieMay: 25 January 2013 - 08:36 AM

Was This Post Helpful? 1
  • +
  • -

#6 artemix22  Icon User is offline

  • D.I.C Head

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

Re: Datagridview Insert Row

Posted 25 January 2013 - 09:01 PM

i try to use both option you gave to me, but no success (it's because i'm not good on this :sweatdrop:/> and I ' m ashamed to ask you further about this), anyway i use another way (not sure if this way is best way in programming), here is what i do :

first i bind all data i need from database to Datagridview1 and than i transfer whole value from Datagridview1 to Datagridview2 with "for each" looping, so whole column in Datagridview2 become unbound, after that i run whole formula i need on Datagridview2 including total, and than simply i use :

Datagridview2.Rows.Add()



to insert total in last row of Datagridview2.

thanks for your help charlie, i really appreciate that :)/>

This post has been edited by artemix22: 25 January 2013 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

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

Re: Datagridview Insert Row

Posted 26 January 2013 - 07:28 PM

What you had was very close, don't be afraid to ask, that's what this forum is about, all we ask is that you show that you are making an attempt to work through the problem.

With what you had, you would add the total to the datarow

Dim myrow = dt.NewRow
myrow(0) = total1
dt.Rows.Add(myrow)


So here's where I think you're having an issue. You actually have two different items in your DGV one is a datarow and the other is a dgv column

Now... Since you have bound data to the grid you can't just add a row with the standard DGV1.Rows.Add, so you need to use the example above to add a new DataRow. Once this is done, the grid has a new row and of that new row, the first column doesn't have a value in the new row but the second column does.

And... Now that you have a new row, you can access it with standard DataGridView methods. So you could then just use

DataGridView1.Rows(DataGridView1.Rows.Count-2).Cells(0).Value = total2


So in closing, each way you have chosen to populate the grid has their own indexes. As you see in both sections of code, each accesses index 0 because each way only has one column.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1