DrawListViewSubItemEventArgs.Graphics Property


Gets the Graphics used to draw the ListViewItem.ListViewSubItem.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public Graphics Graphics { get; }

Property Value

Type: System.Drawing.Graphics

A Graphics used to draw the ListViewItem.ListViewSubItem.

The Graphics class provides properties and methods useful for drawing on the display device.

The following code example demonstrates how to use the Graphics property in an application that provides custom drawing for a ListView control. In the example, a handler for the ListView.DrawSubItem event draws the subitem text values and both the text and background for subitems that have negative values.

For the complete example, see the DrawListViewSubItemEventArgs overview reference topic.

// Draws subitem text and applies content-based formatting.
private void listView1_DrawSubItem(object sender,
    DrawListViewSubItemEventArgs e)
    TextFormatFlags flags = TextFormatFlags.Left;

    using (StringFormat sf = new StringFormat())
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        switch (e.Header.TextAlign)
            case HorizontalAlignment.Center:
                sf.Alignment = StringAlignment.Center;
                flags = TextFormatFlags.HorizontalCenter;
            case HorizontalAlignment.Right:
                sf.Alignment = StringAlignment.Far;
                flags = TextFormatFlags.Right;

        // Draw the text and background for a subitem with a 
        // negative value. 
        double subItemValue;
        if (e.ColumnIndex > 0 && Double.TryParse(
            e.SubItem.Text, NumberStyles.Currency,
            NumberFormatInfo.CurrentInfo, out subItemValue) &&
            subItemValue < 0)
            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if ((e.ItemState & ListViewItemStates.Selected) == 0)

            // Draw the subitem text in red to highlight it. 
                listView1.Font, Brushes.Red, e.Bounds, sf);


        // Draw normal text for a subitem with a nonnegative 
        // or nonnumerical value.

.NET Framework
Available since 2.0
Return to top