Displays text, icons, and tree structures in cells of the winform datagridview.

Source: Internet
Author: User
    • 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.