Handle Custom painting in DataGridView

Thursday, February 12, 2009

How to Handle Custom painting in DataGridView?


The DataGridView control provides several properties that you can use to adjust the appearance and basic behavior (look and feel) of its cells, rows, and columns. If you have requirements that go beyond the capabilities of the DataGridViewCellStyle class, you can perform custom drawing of the cell or row content. To paint cells and rows yourself, you can handle various DataGridView painting events such as RowPrePaint, CellPainting and RowPostPaint.

Paint Parts

One important part of custom painting is the concept of paint parts. The DataGridViewPainParts enumeration is used to specify what parts a cell paints. Enum values can be combined together to have a cell paint or not paint specific parts. Here are the different parts:

PaintPart: Example ForeColor value for retrieved object All :All parts are

paintedBackground: The background of the cell is painted using the cell’s background color

Border :The borders are paintedContentBackground: The background part of the cell’s content is painted.

ContentForeground: The foreground part of the cell’s content is painted

ErrorIcon: The error icon is paintedFocus: The focus rectangle for the cell is painted

Focus :The focus rectangle for the cell is painted

SelectionBackground: The background is painted selected if the cell is selected.

Row Pre Paint and Post Paint

You can control the appearance of DataGridView rows by handling one or both of the DataGridView.RowPrePaint and DataGridView.RowPostPaint events. These events are designed so that you can paint only what you want to while letting the DataGridView control paint the rest. For example, if you want to paint a custom background, you can handle the DataGridView.RowPrePaint event and let the individual cells paint their own foreground content. In the RowPrePaint event you can set the PaintParts event args property to easily customize how the cells paint. For example, if you want to keep cells from painting any selection or focus, your RowPrePaint event would set the PaintParts property like so:

e.PaintParts = DataGridViewPaintParts.All & ~(DataGridViewPaintParts.Focus DataGridViewPaintParts.SelectionBackground);

Which could also be written as:

e.PaintParts = (DataGridViewPaintParts.Background DataGridViewPaintParts.Border DataGridViewPaintParts.ContentBackground DataGridViewPaintParts.ContentForeground DataGridViewPaintParts.ErrorIcon);

Alternately, you can let the cells paint themselves and add custom foreground content in a handler for the DataGridView.RowPostPaint event. You can also disable cell painting and paint everything yourself in a DataGridView.RowPrePaint event handler

we will see more on datagridview in our next posts.

Read more...

How to Use the Hashtable Class in ASP.NET

Tuesday, January 27, 2009

The Hashtable class is perhaps one of the most useful data structures inthe .NET Framework, even if the name is quite unusual. (If you’ve worked inother languages, you might know a type called map. The Hashtable in .NETis the same as a map in other languages.)A Hashtable instance lets you associate data. For example, if you want tostore a list of names and associate a salary with each name, you can use aHashtable. Or, if you want to store a list of vehicle ID numbers — and associateeach with the color of the car — you can use a Hashtable.
Following is some VB code that demonstrates putting a list of car IDs into aHashtable and associating a color with each:



Dim cars As New Hashtable

cars(“6789Q345254”) = “green”

cars(“35T893W2A21”) = “red”

cars(“T9U3W8OQQ53”) = “blue”

cars(“T5WQY374833”) = “blue”

Response.Write(“The car 6789Q345254 is “)

Response.Write(cars(“6789Q345254”))


The first line of this code creates a new Hashtable instance. The next fourlines put some data into the Hashtable instance. You can see the generalformat for inserting the data: You type the name of the Hashtable, followedby a key surrounded by parentheses. In this example, the keys are strings(and are supposed to be vehicle ID numbers, even though I made themshorter than real vehicle ID numbers) — but you can use any type you wantfor the keys.Next comes the equal sign, followed by the value you want to associate withthe key. Again, in this case, I’m using strings, but you’re free to use any type.You can see then that I’m associating the value “green” with the ID
“6789Q345254” using this line:

cars(“6789Q345254”) = “green”


Do you notice anything familiar with the Hashtable? The Hashtable worksvery much like an array — except instead of having to use only integers forthe index, you can use any type you want. (Oh, yes — instead of the termindex, which is associated with a Hashtable, the term is key.)To access the values in the Hashtable, you again put your key inside parentheses,just like you would with an array. Here’s an example line from thepreceding code:


Response.Write(cars(“6789Q345254”))


This code writes out the value for the key “6789Q345254” (which you cansee is “green”).Each element in a Hashtable instance has a unique key. No two elements canshare the same key. However, two elements can share the same value. Think ofthe car example: Two cars won’t share the same vehicle ID number, but twocars can have the same color. Knowing that keys are unique, you can probablyrealize how to change the value in a key: Just reassign it. Suppose you havethe following code (notice that the keys are the same in these two lines):


cars(“T5WQY374833”) = “blue”

cars(“T5WQY374833”) = “red”


Using the Hashtable ClassThe first line associates the string “blue” with a given car. Then the nextline associates the string “red” with the very same car. So which one winsout? The most recent one. The second line simply replaced the old color(blue) with the new color (red) — that is, by changing the value to thestring “red”.One way to understand the Hashtable class — and to recognize when youmight need it — is to think of the class as just an array whose indexes aretypes that don’t have to be integers.The VB codedemonstrates that you can use any type, — notjust integers — for the keys. The code then shows how to iterate throughall the elements in the Hashtable instance. (You can put this code in aPage_Load event to try it out.)
We will See more on Hashtable...

Read more...

Back to TOP