質問

どのように私は、DataGridViewコントロールの個々のセルにErrorProviderにフックすることができますか?

役に立ちましたか?

解決

私はあなたがしかし、DataGridViewのは基本的に同じアイデアだ、それに組み込まれた機能を持って、このようにErrorProviderを使用できることを確認していないよ。

アイデアは単純です。 DataGridViewCellはERRORTEXT性を有しています。あなたがやっていることは、あなたがOnCellValidatingイベントを処理し、検証が失敗した場合、あなたはエラーテキストプロパティを設定し、あなたが赤のエラーアイコンがセルに表示することを取得するには、です。ここではいくつかの擬似コードだ:

public Form1()
{
    this.dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
}

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (!this.Validates(e.FormattedValue)) //run some custom validation on the value in that cell
            {
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Error";
                e.Cancel = true; //will prevent user from leaving cell, may not be the greatest idea, you can decide that yourself.
            }
        }

他のヒント

私はBFreeの溶液で持っている問題は、セルが編集モードのときに何も現れないということですが、私が編集を終了した場合(私の値は、二重であるため)、私は、データ形式エラーが発生します。私はこのようなセルの編集コントロールに直接ErrorProviderを取り付けることで、これを解決します:

private ErrorProvider ep = new ErrorProvider();
private void DGV_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex < 0 || e.RowIndex < 0)
        return;
    double val;
    Control edit = DGV.EditingControl;
    if (edit != null && ! Double.TryParse(e.FormattedValue.ToString(), out val))
    {
        e.Cancel = true;
        ep.SetError(edit, "Numeric value required");
        ep.SetIconAlignment(edit, ErrorIconAlignment.MiddleLeft);
        ep.SetIconPadding(edit, -20); // icon displays on left side of cell
    }
}

private void DGV_CellEndEdt(object sender, DataGridViewCellEventArgs e)
{
    ep.Clear();
}

あなたは自分のBusinessObjectsにIDataErrorInfoを実装し、あまりにもErrorProviderのためのデータソースとしてのBindingSourceを設定することができます。そうすれば、あなたてBusinessObjectのインターンの検証は、DataGridに表示し、すべてのフィールド上のオブジェクトが自動的にバインドされます。

private void myGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    var dataGridView = (DataGridView)sender;
    var cell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
    if ( ... ) // Validation success
    {
        cell.ErrorText = string.Empty;
        return;
    }

    dataGridView.EndEdit();
    cell.ErrorText = error;
    e.Cancel = true;
}

あなたは(たとえば、DataGridViewTextBoxCellから継承)CellTemplateは、独自の実装に設定されているdataGridView.Columnsに(DataGridViewTextBoxColumnのような)列を追加することができます。次に、あなたの実装で - あなたが好きなように検証を処理 - 編集パネルのレンダリングや配置をニーズに合わせて、

。 //:

あなたはでhttpサンプルを確認することができますmsdn.microsoft.com/en-us/library/aa730881(VS.80).aspxするます。

しかし、その後、再び - 。簡単な解決策があるかもしれません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top