- Datagridview in a tree structure: customizing the datagridview to support expanding/collapsing (Ala treegridview) datagridview
-
Treegridview revised version:
Some people reported problems actually running the treegridview sample, so I 've recompiled it on an RTM build machine (previously I was using the latest internal vs build ). you can get the new version (that also includes release & debug binaries prebuilt) here: http://www.windowsforms.net/blogs/markrideout/TreeGridViewRebuilt.zip
- Text and icons are displayed simultaneously in cells.
The datagridview control does not have any built-in support for showing an icon and text in the same cell. through the different painting customization events, such as the cellpaint event, you can easily display an icon next to the text in the cell.
The following example extends the datagridviewtextcolumn and cell to paint an image next to the text. the sample uses the maid. padding property to adjust the text location and overrides the paint method to paint an icon. this sample can be simplified by handling the cellpainting event and other Ming similar code.
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Windows. forms;
Using System. drawing;
Namespace Jrtgenerator. Utility
{
Public Class Textandimagecolumn: maid
{
Private Image imagevalue;
Private Size imagesize;
Public Textandimagecolumn ()
{
This . Celltemplate = New Textandimagecell ();
}
Public Override Object Clone ()
{
Textandimagecolumn C = Base . Clone () As Textandimagecolumn;
C. imagevalue = This . Imagevalue;
C. imagesize = This . Imagesize;
Return C;
}
Public Image
{
Get
{
Return This . Imagevalue;
}
Set
{
If ( This . Image ! = Value)
{
This . Imagevalue = Value;
This . Imagesize = Value. size;
If ( This . Inheritedstyle ! = Null )
{
Padding inheritedpadding = This . Inheritedstyle. padding;
This . Defaultcellstyle. padding = New Padding (imagesize. Width, inheritedpadding. Top, inheritedpadding. Right, inheritedpadding. Bottom );
}
}
}
}
Private Textandimagecell textandimagecelltemplate
{
Get
{
Return This . Celltemplate As Textandimagecell;
}
}
Internal Size imagesize
{
Get
{
Return Imagesize;
}
}
}
Public Class Textandimagecell: maid
{
Private Image imagevalue;
Private Size imagesize;
Public Override Object Clone ()
{
Textandimagecell C = Base . Clone () As Textandimagecell;
C. imagevalue = This . Imagevalue;
C. imagesize = This . Imagesize;
Return C;
}
Public Image
{
Get
{
If ( This . Owningcolumn = Null | This . Owningtextandimagecolumn = Null )
{
Return Imagevalue;
}
Else If ( This . Imagevalue ! = Null )
{
Return This . Imagevalue;
}
Else
{
Return This . Owningtextandimagecolumn. image;
}
}
Set
{
If ( This . Imagevalue ! = Value)
{
This . Imagevalue = Value;
This . Imagesize = Value. size;
Padding inheritedpadding = This . Inheritedstyle. padding;
This . Style. padding = New Padding (imagesize. Width, inheritedpadding. Top, inheritedpadding. Right, inheritedpadding. Bottom );
}
}
}
Protected Override Void Paint (Graphics graphics, rectangle clipbounds, rectangle cellbounds, Int Rowindex, datagridviewelementstates cellstate, Object Value, Object Formattedvalue, String Errortext, datagridviewcellstyle cellstyle, datagridviewadvancedborderstyle advancedborderstyle, datagridviewpaintparts paintparts)
{
// Paint the base content
Base . Paint (graphics, clipbounds, cellbounds, rowindex, cellstate, value, formattedvalue, errortext, cellstyle, advancedborderstyle, paintparts );
If ( This . Image ! = Null )
{
// Draw the image clipped to the cell.
System. Drawing. drawing2d. graphicscontainer container = Graphics. begincontainer ();
Graphics. setclip (cellbounds );
Graphics. drawimageunscaled ( This . Image, cellbounds. Location );
Graphics. endcontainer (container );
}
}
Private Textandimagecolumn owningtextandimagecolumn
{
Get
{
Return This . Owningcolumn As textandimagecolumn;
}< BR >}< br>
}< br>