none
window form 的datagridview可以同一cell裏放圖檔及文字嗎?? RRS feed

  • 問題

  • dear all

    在網頁版裏gridview可以在同一欄中放入圖片及文字

    像這樣

                If lb_CHECK_DESCRIPT.Text.Trim = "" And lb_ALARM_DESCRIPT.Text.Trim <> "" Then
                    m_CBLCHECK_RESULTS.Visible = False
                    txt_CHECK_VALUE.Visible = False
                    If lb_ALARM_NO.Text = "1" Then
                        lb_CHECK_DESCRIPT.Text = "<div><center><img src=""_Image/alarm.png""/> " & _
                                                 "</center></div>" & lb_ALARM_DESCRIPT.Text
                    ElseIf lb_ALARM_NO.Text = "2" Then
                        lb_CHECK_DESCRIPT.Text = "<div><center><img src=""_Image/attention.png""/> " & _
                                                 "</center></div>" & lb_ALARM_DESCRIPT.Text
                    ElseIf lb_ALARM_NO.Text = "3" Then
                        lb_CHECK_DESCRIPT.Text = "<div><center><img src=""_Image/descript.png""/> " & _
                                                 "</center></div>" & lb_ALARM_DESCRIPT.Text
                    Else
                        lb_CHECK_DESCRIPT.Text = lb_ALARM_DESCRIPT.Text
                    End If
                End If

    但在window form 裏 datagridview 做得到嗎??

    如果做不到可以同一欄中某段文字放大嗎??

    比如  " 警告:

            "不可以在....." 

    在同一欄位,:警告兩字放大再換行, 換行已經做好了,但"警告"兩字要放大卻google不到方法..

    感恩!!

    2017年4月14日 下午 02:31

解答

所有回覆

  • 如何:擴充儲存格和資料行的行為和外觀以自訂 Windows Form DataGridView 控制項中的儲存格和資料行

    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    • 已標示為解答 王振賢 2017年4月16日 下午 11:08
    2017年4月15日 下午 03:52
    版主
    • 已標示為解答 王振賢 2017年4月16日 下午 11:08
    2017年4月16日 上午 07:50
  • 謝謝您!!
    2017年4月16日 下午 11:08
  • 謝謝您!!
    2017年4月16日 下午 11:08
  • 以上是我要的結果,以下是class

                    

    Public Class SCC_TEXTANDIMAGE
        Public Class TextAndImageColumn
            Inherits DataGridViewTextBoxColumn
            Private _imageValue As Image
            Private _imageSize As Size
            Public Sub New()
                MyBase.New()
                Me.CellTemplate = New TextAndImageCell
                Me.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            End Sub
            Public Property Image() As Image
                Get
                    Return Me._imageValue
                End Get
                Set(ByVal value As Image)
                    Me._imageValue = value
                    Me._imageSize = value.Size
                    If Me.InheritedStyle IsNot Nothing Then
                        Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
                        Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom) 
                    End If
                End Set
            End Property
            Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell
                Get
                    Return CType(Me.CellTemplate, TextAndImageCell)
                End Get
            End Property
            Friend ReadOnly Property ImageSize() As Size
                Get
                    Return ImageSize
                End Get
            End Property
            Public Overrides Function Clone() As Object
                Dim c As TextAndImageColumn = CType(MyBase.Clone, TextAndImageColumn)
                c._imageValue = Me._imageValue
                c._imageSize = Me.ImageSize
                Return c
            End Function
        End Class

        Public Class TextAndImageCell
            Inherits DataGridViewTextBoxCell
            Private imageValue As Image
            Private imageSize As Size
            Public Property Image() As Image
                Get
                    If ((Me.OwningColumn Is Nothing) OrElse (Me.OwningTextAndImageColumn Is Nothing)) Then
                        Return imageValue
                    ElseIf (Me.imageValue IsNot Nothing) Then
                        Return Me.imageValue
                    Else
                        Return Me.OwningTextAndImageColumn.Image
                    End If
                End Get
                Set(ByVal value As Image)
                    'If Not Me.imageValue.Equals(value) Then
                    Me.imageValue = value
                    Me.imageSize = value.Size
                    Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
                    Me.Style.Padding = New Padding(0, inheritedPadding.Top + 10, inheritedPadding.Right, inheritedPadding.Bottom) 
                    'End If
                End Set
            End Property
            Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn
                Get
                    Return CType(Me.OwningColumn, TextAndImageColumn)
                End Get
            End Property
            Public Overrides Function Clone() As Object
                Dim c As TextAndImageCell = CType(MyBase.Clone, TextAndImageCell)
                c.imageValue = Me.imageValue
                c.imageSize = Me.imageSize
                Return c
            End Function
            Protected Overrides Sub Paint(ByVal graphics As Graphics, _
                                          ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, _
                                          ByVal rowIndex As Integer, _
                                          ByVal cellState As DataGridViewElementStates, _
                                          ByVal value As Object, ByVal formattedValue As Object, _
                                          ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, _
                                          ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
                                          ByVal paintParts As DataGridViewPaintParts)
                MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
                If (Not (Me.Image) Is Nothing) Then
                    Dim container As System.Drawing.Drawing2D.GraphicsContainer = graphics.BeginContainer 
                    graphics.SetClip(cellBounds)
                    'graphics.DrawImageUnscaled(Me.Image, cellBounds.Location)
                    graphics.DrawImageUnscaled(Me.Image, cellBounds.Left + (cellBounds.Left \ 2), cellBounds.Top)
                    graphics.EndContainer(container)
                End If
            End Sub
        End Class

        Public Class CustomDataGridViewCheckBoxColumn
            Inherits DataGridViewCheckBoxColumn
            Public Sub New()
                MyBase.New()
                Me.CellTemplate = New CustomDataGridViewCheckBoxCell
                Me.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
            End Sub
        End Class

        Public Class CustomDataGridViewCheckBoxCell
            Inherits DataGridViewCheckBoxCell 
            Private VisibleValue As Boolean
            Public Property Visible() As Boolean
                Get
                    Return VisibleValue
                End Get
                Set(ByVal value As Boolean)
                    VisibleValue = value
                End Set
            End Property

            ' Override the Clone method so that the Enabled property is copied.
            Public Overrides Function Clone() As Object
                Dim Cell As CustomDataGridViewCheckBoxCell = CType(MyBase.Clone(), CustomDataGridViewCheckBoxCell)
                Cell.Visible = Me.Visible
                Return Cell
            End Function

            ' By default, enable the button cell.
            Public Sub New()
                Me.VisibleValue = True
            End Sub

            Protected Overrides Sub Paint(ByVal graphics As Graphics, _
                                          ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, _
                                          ByVal rowIndex As Integer, _
                                          ByVal elementState As DataGridViewElementStates, _
                                          ByVal value As Object, ByVal formattedValue As Object, _
                                          ByVal errorText As String, _
                                          ByVal cellStyle As DataGridViewCellStyle, _
                                          ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
                                          ByVal paintParts As DataGridViewPaintParts)

                ' The button cell is disabled, so paint the border,  
                ' background, and disabled button for the cell.
                If Not Me.VisibleValue Then
                    ' Draw the background of the cell, if specified.
                    If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
                        Dim cellBackground As New SolidBrush(cellStyle.BackColor)
                        graphics.FillRectangle(cellBackground, cellBounds)
                        cellBackground.Dispose()
                    End If
                    ' Draw the cell borders, if specified.
                    If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
                        PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
                    End If
                    ' Calculate the area in which to draw the button.
                    Dim CheckBoxArea As System.Drawing.Point
                    Dim CheckBoxAdjustment As Rectangle = Me.BorderWidths(advancedBorderStyle)
                    CheckBoxArea.X += CheckBoxAdjustment.X
                    CheckBoxArea.Y += CheckBoxAdjustment.Y

                    ' Draw the disabled button
                    CheckBoxRenderer.DrawCheckBox(graphics, CheckBoxArea, VisualStyles.CheckBoxState.CheckedDisabled)

                    ' Draw the disabled button text. 
                    If TypeOf Me.FormattedValue Is String Then
                        TextRenderer.DrawText(graphics, CStr(Me.FormattedValue), Me.DataGridView.Font, CheckBoxArea, SystemColors.GrayText)
                    End If
                Else
                    ' The button cell is enabled, so let the base class 
                    ' handle the painting.
                    MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
                End If
            End Sub

        End Class

    End Class

    接著是form裏的程式

     '設定格式  
            With DataGridView_show
                .ColumnHeadersVisible = True
                .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
                .RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing
                .ColumnHeadersHeight = 30
                .RowHeadersWidth = 20
                .Font = New Font(New FontFamily("新細明體"), 12)
                .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells
                .DefaultCellStyle.WrapMode = DataGridViewTriState.True
                .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders

                .Columns.Add("LINE_NO", "項次")
                .Columns(.Columns.Count - 1).Width = CInt(m_value * 0.06)
                .Columns(.Columns.Count - 1).ReadOnly = True
                .Columns.Add("SYMBOL", "特別符號")
                .Columns(.Columns.Count - 1).Width = CInt(m_value * 0.1)
                .Columns(.Columns.Count - 1).ReadOnly = True
                .Columns.Add("WORK_CEN", "工作區域")
                .Columns(.Columns.Count - 1).Width = CInt(m_value * 0.1)
                .Columns(.Columns.Count - 1).ReadOnly = True
                .Columns.Add("WUC", "工作單元代號")
                .Columns(.Columns.Count - 1).Width = CInt(m_value * 0.12)
                .Columns(.Columns.Count - 1).ReadOnly = True

                '檢查說明
                Dim M_COL_CHECK_DESCRIPT As New SCC_TEXTANDIMAGE.TextAndImageColumn
                M_COL_CHECK_DESCRIPT.HeaderText = "檢查說明"
                M_COL_CHECK_DESCRIPT.Name = "CHECK_DESCRIPT"
                M_COL_CHECK_DESCRIPT.Width = CInt(m_value * 0.57)
                M_COL_CHECK_DESCRIPT.ReadOnly = True
                .Columns.Add(M_COL_CHECK_DESCRIPT) 

                '是否合格
                Dim m_col_RESULTS0 As New SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxColumn
                m_col_RESULTS0.HeaderText = "無須執行"
                m_col_RESULTS0.Name = "CHECK_RESULTS0"
                m_col_RESULTS0.Width = CInt(m_value * 0.09)
                .Columns.Add(m_col_RESULTS0)

                Dim m_col_RESULTS1 As New SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxColumn
                m_col_RESULTS1.HeaderText = "合格"
                m_col_RESULTS1.Name = "CHECK_RESULTS1"
                m_col_RESULTS1.Width = CInt(m_value * 0.07)
                .Columns.Add(m_col_RESULTS1)

                Dim m_col_RESULTS2 As New SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxColumn
                m_col_RESULTS2.HeaderText = "不合格"
                m_col_RESULTS2.Name = "CHECK_RESULTS2"
                m_col_RESULTS2.Width = CInt(m_value * 0.07)
                .Columns.Add(m_col_RESULTS2)

                .Columns.Add("CHECK_VALUE", "量測值")
                .Columns(.Columns.Count - 1).Width = CInt(m_value * 0.08)
                .Columns.Add("CHECK_NO", "檢查項目")
                .Columns(.Columns.Count - 1).Width = CInt(m_value * 0.2)
                .Columns(.Columns.Count - 1).Visible = False
                .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
                .ColumnHeadersHeight = .ColumnHeadersHeight * 2
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
                .ColumnHeadersDefaultCellStyle.BackColor = Color.White
                .Rows.Clear()
                .Focus()
            End With

    --給資料時

                    '檢查說明_CHECK_DESCRIPT
                    If tb.Rows(i).Item("ALARM_NO").ToString = "1" Then
                        CType(DataGridView_show.Rows(i).Cells(4), SCC_TEXTANDIMAGE.TextAndImageCell).Image = CType(ImageList1.Images(0), Image)
                        DataGridView_show.Rows(i).Cells(4).Value = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & tb.Rows(i).Item("ALARM_DESCRIPT").ToString
                        Dim checkCell_5 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(5), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_5.Visible = False
                        Dim checkCell_6 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(6), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_6.Visible = False
                        Dim checkCell_7 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(7), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_7.Visible = False
                        DataGridView_show.Rows(i).Cells(8).ReadOnly = True
                    ElseIf tb.Rows(i).Item("ALARM_NO").ToString = "2" Then
                        CType(DataGridView_show.Rows(i).Cells(4), SCC_TEXTANDIMAGE.TextAndImageCell).Image = CType(ImageList1.Images(1), Image)
                        DataGridView_show.Rows(i).Cells(4).Value = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & tb.Rows(i).Item("ALARM_DESCRIPT").ToString
                        Dim checkCell_5 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(5), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_5.Visible = False
                        Dim checkCell_6 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(6), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_6.Visible = False
                        Dim checkCell_7 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(7), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_7.Visible = False
                        DataGridView_show.Rows(i).Cells(8).ReadOnly = True
                    ElseIf tb.Rows(i).Item("ALARM_NO").ToString = "3" Then
                        CType(DataGridView_show.Rows(i).Cells(4), SCC_TEXTANDIMAGE.TextAndImageCell).Image = CType(ImageList1.Images(2), Image)
                        DataGridView_show.Rows(i).Cells(4).Value = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & tb.Rows(i).Item("ALARM_DESCRIPT").ToString
                        Dim checkCell_5 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(5), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_5.Visible = False
                        Dim checkCell_6 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(6), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_6.Visible = False
                        Dim checkCell_7 As SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell = CType(DataGridView_show.Rows(i).Cells(7), SCC_TEXTANDIMAGE.CustomDataGridViewCheckBoxCell)
                        checkCell_7.Visible = False
                        DataGridView_show.Rows(i).Cells(8).ReadOnly = True
                    Else
                        DataGridView_show.Rows(i).Cells(4).Value = tb.Rows(i).Item("CHECK_DESCRIPT").ToString
                        DataGridView_show.Rows(i).Cells(5).ReadOnly = False
                        DataGridView_show.Rows(i).Cells(6).ReadOnly = False
                        DataGridView_show.Rows(i).Cells(7).ReadOnly = False
                        DataGridView_show.Rows(i).Cells(8).ReadOnly = False
                    End If

    --grid head

        Private Sub DataGridView_show_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView_show.Paint
            Dim r1 As Rectangle
            ' Fill-in the empty space above the column 0 header
            r1 = DataGridView_show.GetCellDisplayRectangle(5, -1, True)
            r1.X += 1
            r1.Y += 1
            r1.Width = r1.Width * 3 - 2
            r1.Height = r1.Height / 2 - 1
            e.Graphics.FillRectangle(New SolidBrush(Color.White), r1)
            Dim format As StringFormat
            format = New StringFormat()
            format.Alignment = StringAlignment.Center
            format.LineAlignment = StringAlignment.Center
            e.Graphics.DrawString("是否合格", DataGridView_show.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.DataGridView_show.ColumnHeadersDefaultCellStyle.ForeColor), r1, format)
        End Sub

    以上給需要的人參考..

    謝謝大家!!

    2017年4月21日 下午 02:03
  • 給你一個建議,如果可能的話,切換到 WPF 去,搞這些東西方便多了。

    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。


    2017年4月21日 下午 04:34
    版主
  • 謝謝您!!
    2017年4月23日 上午 10:50