[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Datagrid View - Cell Padding and adding an icon

Posted on 2006-07-11
12
Medium Priority
?
1,127 Views
Last Modified: 2008-02-01
Hi there

I have a datagrid view, one column and many rows.  For each cell I want to pad the cell so the text is indented by a few pixels and display a status icon *before* the text.

I thought below was the most obvious to indent a cell:

DataGridView1.DefaultCellStyle.Padding.Left = 80

but I get the error message:  Expression is a value and therefore cannot be the target of an assignment.

And there doesn't seem to be an option to show a background image on a cell :oS

Any ideas?

0
Comment
Question by:DavidGreenfield
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17086513
1) The Padding property is a structure, so try this instead:
    Me.DataGridView1.DefaultCellStyle.Padding = New Padding(80, 0, 0, 0)

2) Handle the CellPainting to customize a cell:

   Private Sub CellPainter(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
      e.Graphics.DrawImage(Image.FromFile("C:\Windows\Zapotec.bmp"), 0, 0)
   End Sub

Bob
0
 

Author Comment

by:DavidGreenfield
ID: 17088691
Hi Bob

1) works fantastically well!

2) When I tried this the image doesn't appear in all cells, and when I move the scroll bar up and down it seems to add and remove the image randomly against the cells.  How do I stop that happening?

Thanks so far!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17089938
2) That sounds like the graphics are not persisting.  When you scroll, does the CellPainting event fire?

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:DavidGreenfield
ID: 17090337

Yes it does appear that the cell painting is firing each time I scroll
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17090387
Can you show me what you came up with, then?

Bob
0
 

Author Comment

by:DavidGreenfield
ID: 17090517
Not sure I understand

To check it fired I just output the time in the debug window and checked on scrolling that this piece of code was being fired.  Otherwise on moving between screens it also fires - so i can't walk through the code.

The output is exactly the same, on scrolling sometimes the picture is shown, sometimes not.  Because it fires so much I can't tell how many times it fires against each cell on a scroll.  On one mouse click on the scroll there appears to be 2 fires however :oS
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17091025
I am sorry for the unclear question.  Can you show me the code that you came up with to solve the problem?  I want to make sure that the implementation is what it needs to be to get the job done correctly.

Bob
0
 

Author Comment

by:DavidGreenfield
ID: 17091712
I have not actually solved the problem yet

I have a split panel with left docking, flexgrid in the top panel.  I am adding data to the grid using

DataGridView1.Rows.Add(menu_xml_node.Attributes.GetNamedItem("title").Value)

then I have the below:

    Private Sub CellPainter(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        e.Graphics.DrawImage(My.Resources.arrow, 0, 0)
        Debug.Print(System.DateTime.Now())
    End Sub

initially before I have started scrolling only the top row has an image in its cell.  On scrolling they randomly appear in different rows.

Thanks so far






0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17091928
David,

Is the image the only thing in the cell, is there something like text?

Bob
0
 

Author Comment

by:DavidGreenfield
ID: 17092309
Yes there is text in the cell as well.  Will try it without text
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 17092349
I believe that when you are using the CellPainting, you need to handle the entire cell yourself.

There are methods available through the DataGridViewCellPaintingEventArgs to make this easier, like Paint, PaintBackground, PaintContent.  These allow you to control when each painting step takes places, so that drawing the text doesn't clear the image from the cell.

Bob
0
 

Author Comment

by:DavidGreenfield
ID: 17092374

Nope, doesn't seem to be making a difference.  Have tried it in another grid with no formating etc on and still getting the same problem.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question