強調表示されたセルの内容を消去するために、DataGridViewで削除機能を実装しようとしています。データバインドされたデータグリッドビューでセルの内容をクリアする
カラムの1つにdoubleが含まれ、値が0より小さい場合は空白として表示されます。ユーザーがセルを空白に編集すると、これはCellParsingイベントによって処理されます。
DataGridViewは、BindingSourceとBindingListを使用してデータバインドされています。
私の問題は、clear関数でセルの値を空白に変更すると、CellParsingイベントが発生せず、Doubleに有効な値ではないというFormatExceptionが発生します。ユーザーがセルを消去すると、CellParsingイベントが発生し、すべてが期待通りに行われます。
値を空白に設定する理由は、列の一部がテキストであり、他が数値なので、すべてを一度に削除したいと考えています。
私はGoogle検索し、StackOverflowを検索し、まだ私の問題を解決するものを見つけていない。 CellParsingイベントや他の明白な解決策を使ってこれをルーティングする方法はありますか?
下記のCellParsing and clearing codeを参照してください。
System::Void dataGridViewWells_CellParsing(System::Object^ sender, System::Windows::Forms::DataGridViewCellParsingEventArgs^ e)
{
//Handle blank values in the mass column
e->ParsingApplied = false;
if(this->dataGridViewWells->Columns[e->ColumnIndex]->HeaderText == "Mass (ng)")
{
if(e->Value->ToString() == "" || e->Value->ToString() == " ")
{
e->Value = -1.0;
e->ParsingApplied = true;
}
}
}
void DeleteHighlightedCells(DataGridView^ dgv)
{
try
{
System::Windows::Forms::DataGridViewSelectedCellCollection^ sCells = dgv->SelectedCells;
for(int i = 0; i < sCells->Count; i++)
{
if(!sCells[i]->ReadOnly)
{
dgv->Rows[sCells[i]->RowIndex]->Cells[sCells[i]->ColumnIndex]->Value = "";
}
}
}
catch(Exception^ e)
{
LogError("Unable to delete contents of DataGridView cells: " + e->ToString());
}
}
System::Void dataGridViewWells_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
{
if(e->Control && e->KeyCode == Keys::C)
{
this->CopyContentsToClipBoard(this->dataGridViewWells);
}
if(e->Control && e->KeyCode == Keys::V)
{
this->PasteContentsFromClipBoard(this->dataGridViewWells);
}
if(e->KeyCode == Keys::Delete)
{
this->DeleteHighlightedCells(this->dataGridViewWells);
}
}