请帮我在数据网格视图Visual Basic的单元格内制作一个图形吗? [英] Please help me make a graph inside a cell in data grid view visual basic...?

查看:265
本文介绍了请帮我在数据网格视图Visual Basic的单元格内制作一个图形吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我在Visual Basic的数据网格视图中的单元格内制作图形(如附图中的图形)
我尝试使用此代码,但没有用.

[

Please help me make a graph inside a cell in data grid view in Visual Basic (like the graphic in the attached picture)
I tried to use this code but did not work.

[^]

What I have tried:

#Region "import name spaces"
Imports System.Drawing
Imports System.ComponentModel

#End Region

#Region "define columns"

Public Class DataGridViewProgressColumn
    Inherits DataGridViewImageColumn
    Public Sub New()
        Me.CellTemplate = New DataGridViewProgressCell
    End Sub
End Class

#End Region

#Region "define cells"

Public Class DataGridViewProgressCell
    Inherits DataGridViewImageCell

    Sub New()
        ValueType = Type.GetType("double")
    End Sub
    ''Method required To make the Progress Cell consistent With the Default Image Cell. 
    ''The Default Image Cell assumes an Image As a value, although the value Of the Progress Cell Is an Integer.
    Protected Overrides Function GetFormattedValue(ByVal value As Object,
                                                   ByVal rowIndex As Integer,
                                                   ByRef cellStyle As DataGridViewCellStyle,
                                                   ByVal valueTypeConverter As TypeConverter,
                                                   ByVal formattedValueTypeConverter As TypeConverter,
                                                   ByVal context As DataGridViewDataErrorContexts) As Object

        Static emptyImage As Bitmap = New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        GetFormattedValue = emptyImage
    End Function

    Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
                                  ByVal clipBounds As System.Drawing.Rectangle,
                                  ByVal cellBounds As System.Drawing.Rectangle,
                                  ByVal rowIndex As Integer,
                                  ByVal cellState As System.Windows.Forms.DataGridViewElementStates,
                                  ByVal value As Object,
                                  ByVal formattedValue As Object,
                                  ByVal errorText As String,
                                  ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle,
                                  ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle,
                                  ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)

        Dim progressVal As Integer = CType(value, Integer)
        Dim percentage As Single = CType((progressVal / 100), Single)
        Dim backBrush As Brush = New SolidBrush(cellStyle.BackColor)
        Dim foreBrush As Brush = New SolidBrush(cellStyle.ForeColor)

        '' Call the base class method to paint the default cell appearance.
        MyBase.Paint(g, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)

        If percentage > 0.0 Then
            '' Draw the progress bar and the text
            g.FillRectangle(New SolidBrush(Color.FromArgb(163, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4)

            g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
        Else
            ''draw the text
            If Not Me.DataGridView.CurrentCell Is Nothing AndAlso Me.DataGridView.CurrentCell.RowIndex = rowIndex Then

                g.DrawString(progressVal.ToString() & "%", cellStyle.Font, New SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2)
            Else
                g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
            End If
        End If
    End Sub
end class

推荐答案

本文讨论了自定义格式单元格: ^ ].您可以使用此技术来返回自定义图像,例如您要实现的图像.

尽管图形类型不同,另一种选择是使用微型图形字体,例如洪水之后 [ OpenType字体 [ ^ ]在Winform Apps中.
This article talks about custom formatting cells: Add Formatting to Grid Cells and Rows for C# GridView Control in WinForms Applications[^]. You can use this technique to return a custom image like the one that you are trying to achieve.

Another choice, although different types of graphs, is to use a micro graphing font like After the flood[^]. This article (with sample code) explains how to use OpenType Fonts[^] in Winform Apps.


这篇关于请帮我在数据网格视图Visual Basic的单元格内制作一个图形吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆