【发布时间】:2017-06-05 23:00:42
【问题描述】:
我正在尝试在我的 DataGridView 中实现删除功能以清除突出显示的单元格的内容。
其中一列包含一个双精度值,当该值小于零时,我将其显示为空白。如果用户将单元格编辑为空白,则通过 CellParsing 事件进行处理。
DataGridView 使用 BindingSource 和 BindingList 进行数据绑定。
我遇到的问题是,当我通过 clear 函数将单元格值更改为空白时,CellParsing 事件不会触发,并且我收到一个 FormatException 说“”不是 Double 的有效值。当用户清除单元格时,会触发 CellParsing 事件,一切都会按预期发生。
我将值设置为空白的原因是某些列是文本,而其他列是数字,我希望能够一次将它们全部删除。
我已经通过 StackOverflow 进行了谷歌搜索和搜索,但尚未找到可以解决我的问题的内容。有没有办法通过 CellParsing 事件或我缺少的其他一些明显的解决方案来路由它?
请参阅下面的 CellParsing 和清除代码。
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);
}
}
【问题讨论】:
标签: c# winforms visual-c++ datagridview