2017-06-05 84 views
0

我想在我的DataGridView中實現刪除功能以清除突出顯示的單元格的內容。清除數據綁定datagridview中的單元格內容

其中一列包含一個double,當該值小於零時,我將其顯示爲空白。如果用戶將單元格編輯爲空白,則通過CellParsing事件處理。

DataGridView是使用BindingSource和BindingList的數據綁定。

我遇到的問題是,當我通過我的清除函數將單元格值更改爲空白時,CellParsing事件不會觸發,並且我得到一個FormatException,表示「」不是Double的有效值。當用戶清除單元格時,CellParsing事件觸發,並且所有事情都按預期發生。

我將該值設置爲空白的原因是,某些列是文本,其他列是數字,我希望能夠一次刪除它們。

我用Google搜索並通過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); 
    } 
} 

回答

0

我最終放棄CellParsing事件,並把在自定義類型轉換器爲我的類屬性爲在this question完成。

的TypeConverter代碼:

public ref class MassTypeConverter : System::ComponentModel::TypeConverter 
{ 
public: 
    virtual bool CanConvertFrom(System::ComponentModel::ITypeDescriptorContext^ context, Type^ sourceType) override 
    { 
     return sourceType == String::typeid || System::ComponentModel::TypeConverter::CanConvertFrom(context, sourceType); 
    } 
    virtual Object^ ConvertFrom(System::ComponentModel::ITypeDescriptorContext^ context, System::Globalization::CultureInfo^ culture, Object^ value) override 
    { 
     if(value != nullptr && value->GetType() == String::typeid) 
     { 
      // 
      double converted; 
      bool success = double::TryParse((String^)value, converted); 
      if(((String^)value) == "" || ((String^)value) == " ") 
      { 
       converted = -1.0; 
       success = true; 
      } 
      if(success) 
      { 
       return converted; 
      } 
     } 
     return System::ComponentModel::TypeConverter::ConvertFrom(context, culture, value); 
    } 
}; 

然後把我上面的類屬性如下:

[System::ComponentModel::TypeConverter(MassTypeConverter::typeid)] 
相關問題