2017-06-05 6 views
0

強調表示されたセルの内容を消去するために、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); 
    } 
} 

答えて

0

私はCellParsingイベントを放棄し、this questionで行ったように私のクラスのプロパティのためのカスタムのTypeConverterに入れてしまいました。

の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)] 
関連する問題