11 Replies - 2141 Views - Last Post: 07 February 2012 - 12:13 PM Rate Topic: -----

#1 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Graphic DtaTaGridView problem

Posted 05 February 2012 - 02:40 PM

I treid this code to draw the cells value of DataGridView on the form. but it display the three cells in the same place
means: if the cells including A-B-C it didn't showed them like this : A B C
[code]For i As Integer = 0 To 2
            
                Dim g As Graphics
                g = Me.CreateGraphics()
                Dim str As String = Me.DataGridView1.Rows(1).Cells(i).Value

                g.DrawString(str, New Font("Times New Roman", 12, FontStyle.Regular), Brushes.DarkGreen, 20, 20)
           
        Next
[/code]

This post has been edited by Mangore: 05 February 2012 - 02:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Graphic DtaTaGridView problem

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: Graphic DtaTaGridView problem

Posted 06 February 2012 - 05:13 AM

first: for each i in for loop your variable str is overwrite with new value. You must initialize variable str outside for-loop. Then use
str &= Me.DataGridView1.Rows(1).Cells(i).Value

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: Graphic DtaTaGridView problem

Posted 06 February 2012 - 05:22 AM

Your problem is here:
g.DrawString(str, New Font("Times New Roman", 12, FontStyle.Regular), Brushes.DarkGreen, 20, 20)

What do the two numbers at the end represent? The answer to this question should help you understand why they're drawing in the same location.

This post has been edited by CharlieMay: 06 February 2012 - 05:23 AM

Was This Post Helpful? 1
  • +
  • -

#4 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Graphic DtaTaGridView problem

Posted 06 February 2012 - 10:14 AM

I tried this way but it displayed data(values) as inclined like this :

Tom
..... Pop
........... L.A
.......... .....etc

 Private str As String
 Dim g As Graphics
 g = Me.CreateGraphics()

        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            For j = 0 To DataGridView1.ColumnCount - 1

                str = Me.DataGridView1.Rows(i).Cells(j).Value
                Dim drawFormat As New StringFormat()
                Dim y, X As Integer
                y += 20
                X += 30
                g.DrawString(str, New Font("Times New Roman", 12, FontStyle.Regular), Brushes.DarkGreen, X, y)
            Next
        Next

This post has been edited by Mangore: 06 February 2012 - 10:38 AM

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: Graphic DtaTaGridView problem

Posted 06 February 2012 - 10:37 AM

OK, now you're setting y to 0 and X to 0 and then adding 20 and 30 to them respectively, 0+20 = 20 each time. Move the Dim y, X as Integer up above the for i... loop and see if that yields better results.

HOWEVER...
If you want to display A B C then why would you do anything with the Y axis? It should stay constant at the location you specify.
Was This Post Helpful? 1
  • +
  • -

#6 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Graphic DtaTaGridView problem

Posted 06 February 2012 - 12:28 PM

I tried this way and did same thing as it did in previews code
 Dim g As Graphics
        g = Me.CreateGraphics()
        Dim y As Integer = 0
        Dim X As Integer = 0

        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            For j = 0 To DataGridView1.ColumnCount - 1

                str = Me.DataGridView1.Rows(i).Cells(j).Value
                Dim drawFormat As New StringFormat()
                
                y += 20
                X += 30
                g.DrawString(str, New Font("Times New Roman", 12, FontStyle.Regular), Brushes.DarkGreen, X, y)
            Next
        Next

This post has been edited by Mangore: 06 February 2012 - 12:30 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: Graphic DtaTaGridView problem

Posted 06 February 2012 - 01:17 PM

Dim y as Integer = 20

Then take out the line y += 20

You should not be incrementing y as it is all supposed to be on the same line.

EDIT:
I guess I should say, you don't want to increment Y in the j loop, you should increment in the i loop

Oh yea, and in your i loop, don't forget to set X back to 0 or it will continue on the X-Axis from it's last value.

This post has been edited by CharlieMay: 06 February 2012 - 01:31 PM

Was This Post Helpful? 1
  • +
  • -

#8 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Graphic DtaTaGridView problem

Posted 06 February 2012 - 03:22 PM

For Y work with me but for X I don't know how to do it
Dim g As Graphics
        g = Me.CreateGraphics()
        Dim y As Integer = 20
        Dim X As Integer = 0

        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            y += 20

            For j = 0 To DataGridView1.ColumnCount - 1

                str = Me.DataGridView1.Rows(i).Cells(j).Value
                Dim drawFormat As New StringFormat()
                X += 30


                g.DrawString(str, New Font("Times New Roman", 12, FontStyle.Regular), Brushes.DarkGreen, X, y)
            Next
        Next

Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

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

Re: Graphic DtaTaGridView problem

Posted 06 February 2012 - 03:56 PM

To reset back to 0 just use:
X = 0

You can do that right after your y += 20
Was This Post Helpful? 1
  • +
  • -

#10 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Graphic DtaTaGridView problem

Posted 07 February 2012 - 10:12 AM

View PostCharlieMay, on 06 February 2012 - 03:56 PM, said:

To reset back to 0 just use:
X = 0

You can do that right after your y += 20

I did this way,and it display the data in many Rows, so it succeed for Rows, but displayed the data in the same column (one column). not as it show in DGV
I hope I'm clear.
Dim g As Graphics
g = Me.CreateGraphics()
        Dim y As Integer = 20
        Dim X As Integer = 0

        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            y += 20
            X = 0
            For j = 0 To DataGridView1.ColumnCount - 1

                str = Me.DataGridView1.Rows(i).Cells(j).Value
                g.DrawString(str, New Font("Times New Roman", 12, FontStyle.Regular), Brushes.DarkGreen, X, y)
            Next
        Next


This post has been edited by Mangore: 07 February 2012 - 10:15 AM

Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

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

Re: Graphic DtaTaGridView problem

Posted 07 February 2012 - 11:03 AM

why did you remove the x increment? x increments the position of the columns. Put the x+=30 back where you had it and try that.

the only reason we reset x in the i loop is because when we start a new row, we want to start back to the position where the previous row started.
Was This Post Helpful? 1
  • +
  • -

#12 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Graphic DtaTaGridView problem

Posted 07 February 2012 - 12:13 PM

in the first thank you for help!!
This code work work with me, but frankly I didn't understand what I did. specially about Y and X.may you help me in understand it?
Dim g As Graphics
        g = Me.CreateGraphics()
        Dim y As Integer = 20
        Dim X As Integer = 0

        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            y += 20
            X = 0
            For j = 0 To DataGridView1.ColumnCount - 1

                str = Me.DataGridView1.Rows(i).Cells(j).Value
                X += 30
                g.DrawString(str, New Font("Times New Roman", 12, FontStyle.Regular), Brushes.DarkGreen, X, y)
            Next
        Next

This post has been edited by Mangore: 07 February 2012 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1