【问题标题】:Clear cell contents in a data bound datagridview清除数据绑定datagridview中的单元格内容
【发布时间】: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


    【解决方案1】:

    我最终放弃了 CellParsing 事件并为我的类属性添加了一个自定义 TypeConverter,就像在 this question 中所做的那样。

    类型转换器代码:

    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)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多