Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I show the error icon when the user is editing the cell?

Sometimes when using the error text and icon feature you want an immediate feedback to the user that something that they typed into a cell is incorrect. By default when setting the ErrorText property the error icon will not appear if the cell is in edit mode such as a text box or combo box cell. The below sample demonstrates how you can set a cell's padding in the CellValidating event to provide spacing for the error icon. Since padding by default affects the location of the error icon the sample uses the CellPainting to move the position of the icon for painting. Lastly, the sample uses the tooltip control to display a custom tooltip when the mouse is over the cell to indicate what the problem is. This sample could also be written as a custom cell that overrides GetErrorIconBounds method to provide a location for the error icon that was independent of the padding.

private ToolTip errorTooltip;
private Point cellInError = new Point(-2, -2);

public Form1()
{
  InitializeComponent();
  dataGridView1.ColumnCount = 3;
  dataGridView1.RowCount = 10;
}

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
  if (dataGridView1.IsCurrentCellDirty)
  {
    if (e.FormattedValue.ToString() == "BAD")
    {
      DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
      cell.ErrorText = "Invalid data entered in cell";
      // increase padding for icon. This moves the editing control
      if (cell.Tag == null)
      {
        cell.Tag = cell.Style.Padding;
        cell.Style.Padding = new Padding(0, 0, 18, 0);
        cellInError = new Point(e.ColumnIndex, e.RowIndex);
      }
      if (errorTooltip == null)
      {
        errorTooltip = new ToolTip();
        errorTooltip.InitialDelay = 0;
        errorTooltip.ReshowDelay = 0;
        errorTooltip.Active = false;
      }
    e.Cancel = true;
    }
  }
}

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
  if (dataGridView1.IsCurrentCellDirty && !String.IsNullOrEmpty(e.ErrorText))
  {
    // paint everything except error icon
    e.Paint(e.ClipBounds, DataGridViewPaintParts.All & 
    ~(DataGridViewPaintParts.ErrorIcon));
    // now move error icon over to fill in the padding space
    GraphicsContainer container = e.Graphics.BeginContainer();
    e.Graphics.TranslateTransform(18, 0);
    e.Paint(this.ClientRectangle, DataGridViewPaintParts.ErrorIcon);
    e.Graphics.EndContainer(container);
    e.Handled = true;
  }
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
  if (dataGridView1[e.ColumnIndex, e.RowIndex].ErrorText != String.Empty)
  {
    DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
    cell.ErrorText = String.Empty;
    cellInError = new Point(-2,-2);
    // restore padding for cell. This moves the editing control
    cell.Style.Padding = (Padding)cell.Tag;
    // hide and dispose tooltip 
    if (errorTooltip != null)
    {
      errorTooltip.Hide(dataGridView1);
      errorTooltip.Dispose();
      errorTooltip = null;
    }
  }
}

// show and hide the tooltip for error
private void dataGridView1_CellMouseMove(object sender, 
    DataGridViewCellMouseEventArgs e)
{
  if (cellInError.X == e.ColumnIndex &&
      cellInError.Y == e.RowIndex)
  {
    DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
    if (cell.ErrorText != String.Empty)
    {
      if (!errorTooltip.Active)
      {
        errorTooltip.Show(cell.ErrorText, dataGridView1, 1000);
      }
      errorTooltip.Active = true;
    }
  }
}

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
  if (cellInError.X == e.ColumnIndex &&
    cellInError.Y == e.RowIndex)
  {
    if (errorTooltip.Active)
    {
      errorTooltip.Hide(dataGridView1);
      errorTooltip.Active = false;
    }
  }
}


Page view counter