0
votes

Basically what I have is a datatable that is bound to a DataGridView. What I want to happen is to show a error type tooltip in each of the cells in the datagrid if some exception is true.

I have been able to get the red error alert to show in the cells using CellValidating. The problem is that the user would have to click on a cell and then take that cell out of focus for the alert to become visible.

I also tried using ColumnChanging on the datatable to set the RowError but that didn't work at all. Here is some sample code that I had tried.

importGrid is my DataGridView, csvData is my DataTable

private void importGrid_CellValidating(object sender, 
                                       DataGridViewCellValidatingEventArgs e)
{
  this.importGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText ="Drive Letter in use";
}

private void csvData_ColumnChanging(object sender, 
                                    System.Data.DataColumnChangeEventArgs e)
{
   e.Row.RowError = "test error";
   e.Row.SetColumnError(e.Column, "test error 2");
}
2

2 Answers

1
votes

Why don't you loop over all Rows and Cells after the binding, test the condition and set the ErrorText?

foreach(DataGridViewRow row in DGV.Rows)
foreach(DataGridViewCell cell in row.Cells) 
    if (yourCondition) cell.ErrorText = yourErrorText;
0
votes

There are two events you might find useful. RowErrorTextNeeded and CellErrorTextNeeded. Here is an example from one of my projects using RowErrorTextNeeded. This event is fired by the DataGridView when the row becomes visible, or anything has changed.

private void dataGridView1_RowErrorTextNeeded( object sender, DataGridViewRowErrorTextNeededEventArgs e )
{
    DataGridView dataGridView = sender as DataGridView;
    if( dataGridView != null )
    {
        DataRowView view = dataGridView.Rows[e.RowIndex].DataBoundItem as DataRowView;
        if( view != null )
        {
            if( view.Row[invColorColumn] == DBNull.Value )
                e.ErrorText = "Color code is missing from part database.";
            else if( view.Row[invThickColumn] == DBNull.Value )
                e.ErrorText = "Thickness is missing from part database.";
            else if( view.Row[invWidthColumn] == DBNull.Value )
                e.ErrorText = "Width is missing from part database.";
            else if( view.Row[invHeightColumn] == DBNull.Value )
                e.ErrorText = "Height is missing from part database.";
            else 
                e.ErrorText = String.Empty;
        }
    }
}