【问题标题】:How can I display error icon in cell of datagridview如何在 datagridview 的单元格中显示错误图标
【发布时间】:2015-04-01 17:34:08
【问题描述】:

我正在使用 DataGridView。 如果单元格为空,我需要在 datagridview 的每个单元格中显示错误图标。 单元格在按钮单击时进行验证,并且需要显示错误图标。

任何人都可以分享一些关于相同的想法。

【问题讨论】:

    标签: c#-4.0


    【解决方案1】:

    您可以使用CellPainting event 对单元格进行自定义视觉效果。仅仅为一列添加一个图标是很多工作,但MSDN provides a lot of example code 你可以窃取和修改以满足你自己的需要。

    【讨论】:

      【解决方案2】:

      如果您需要在用户在单元格中输入错误值时使用错误文本和图标来通知用户。默认情况下,设置 ErrorText 属性时,如果单元格处于编辑模式,则不会出现错误图标。

      以下示例演示了如何在 CellValidating 事件中设置单元格的填充以提供错误图标的间距。由于默认情况下填充会影响错误图标的位置,因此示例使用 CellPainting 移动图标的位置进行绘制。

      Imports System
      Imports System.Collections.Generic
      Imports System.ComponentModel
      Imports System.Data
      Imports System.Drawing
      Imports System.Text
      Imports System.Windows.Forms
      Imports System.Drawing.Drawing2D
      
      Partial Public Class Form1
      
          Inherits Form
      
          Private cellInError As New Point(-2, -2)
      
          Public Sub New()
      
              InitializeComponent()
      
          End Sub
      
          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
              Me.DataGridView1.ColumnCount = 3
      
              Me.DataGridView1.RowCount = 10
      
          End Sub
      
       
      
          Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
      
              If DataGridView1.IsCurrentCellDirty Then
      
                  If e.FormattedValue.ToString() = "TEST" Then
      
                      Dim cell As DataGridViewCell = DataGridView1(e.ColumnIndex, e.RowIndex)
      
                      cell.ErrorText = "Invalid data entered in cell"
      
                      If cell.Tag Is Nothing Then
      
                          cell.Tag = cell.Style.Padding
      
                          cell.Style.Padding = New Padding(0, 0, 18, 0)
      
                          cellInError = New Point(e.ColumnIndex, e.RowIndex)
      
                      End If
      
                      e.Cancel = True
      
                  End If
      
              End If
      
          End Sub
      
      
          Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
      
              If DataGridView1(e.ColumnIndex, e.RowIndex).ErrorText <> String.Empty Then
      
                  Dim cell As DataGridViewCell = DataGridView1(e.ColumnIndex, e.RowIndex)
      
                  cell.ErrorText = String.Empty
      
                  cellInError = New Point(-2, -2)
      
                  cell.Style.Padding = DirectCast(cell.Tag, Padding)
      
              End If
      
          End Sub
      
       
          Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
      
              If DataGridView1.IsCurrentCellDirty AndAlso Not String.IsNullOrEmpty(e.ErrorText) Then
      
                  e.Paint(e.ClipBounds, DataGridViewPaintParts.All And Not (DataGridViewPaintParts.ErrorIcon))
      
                  Dim container As GraphicsContainer = e.Graphics.BeginContainer()
      
                  e.Graphics.TranslateTransform(18, 0)
      
                  e.Paint(Me.ClientRectangle, DataGridViewPaintParts.ErrorIcon)
      
                  e.Graphics.EndContainer(container)
      
                  e.Handled = True
      
              End If
      
          End Sub
      
      End Class
      

      【讨论】:

        猜你喜欢
        • 2010-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-23
        • 1970-01-01
        相关资源
        最近更新 更多