Vb.net 重写DataGridViewCell.GetClipboardContent--我应该支持(重新实现)格式细节吗?

Vb.net 重写DataGridViewCell.GetClipboardContent--我应该支持(重新实现)格式细节吗?,vb.net,datagridview,clipboard,Vb.net,Datagridview,Clipboard,我的一些DataGridViewCells在其GetClipboardContent方法中返回了错误的值。它们是DataGridViewComboBoxColumn单元格中的单元格,因此它们使用的是显示的属性,而不是单元格的值属性。我希望它返回值本身 最初的尝试只是使用 Protected Overrides Function GetClipboardContent(ByVal rowIndex As Integer, ByVal firstCell As Boolean, ByVal la

我的一些
DataGridViewCells
在其
GetClipboardContent
方法中返回了错误的值。它们是
DataGridViewComboBoxColumn
单元格中的单元格,因此它们使用的是显示的属性,而不是单元格的值属性。我希望它返回值本身

最初的尝试只是使用

  Protected Overrides Function GetClipboardContent(ByVal rowIndex As Integer, ByVal firstCell As Boolean, ByVal lastCell As Boolean, ByVal inFirstRow As Boolean, ByVal inLastRow As Boolean, ByVal format As String) As Object
    Return Value
  End Function
在我的
datagridviewcomboxcell
子代中,我注意到,DataGridView标准支持的每种数据格式,即format=“HTML”、“Text”、“UnicodeText”和“Csv”,对每个单元格值调用此方法不止一次

对于CSV,如果不是最后一个单元格,基础实现追加逗号,对于HTML,它根据表/表行中的第一行/最后一行/单元格添加正确的标签等。我认为这种格式是特定的,而不是单元格值特定的。p>
那么,我如何替换最后出现在剪贴板中的,而不重新实现所有那些特定于格式的方面呢?这将导致相当多的代码用于基类中已经存在的功能,不是吗?

简短回答:Handle
CellFormatting
,如果在剪贴板“值获取”过程中请求格式化,则返回未格式化的值而不是显示值。
为了能够检测到这一点,让combobox列创建一个combobox单元格子体,当调用其
GetClipboardContent
方法时,该子体在列的属性中设置一个标志

长答覆:

Mh,基本方法似乎是这样做的(来自Mono的c代码):

受保护的虚拟对象GetClipboardContent(int-rowIndex、bool-firstCell、bool-lastCell、bool-inFirstRow、bool-inLastRow、字符串格式){
如果(DataGridView==null)
返回null;
if(rowIndex<0 | | rowIndex>=DataGridView.RowCount)
抛出新ArgumentOutOfRangeException(“rowIndex”,“指定的参数超出了有效值的范围。”);
字符串值=null;
如果(选定){
DataGridViewCellStyle=GetInheritedStyle(null,rowIndex,false);
value=GetEditedFormattedValue(行索引、DataGridViewDataErrorContexts.ClipboardContent | DataGridViewDataErrorContexts.Formatting)作为字符串;
}
如果(值==null)
value=string.Empty;
字符串表\u前缀=string.Empty,单元格\u前缀=string.Empty,行\u前缀=string.Empty;
字符串表_后缀=string.Empty,单元格_后缀=string.Empty,行_后缀=string.Empty;
if(format==DataFormats.unicodext | | format==DataFormats.Text){
如果(lastCell&!inLastRow)
单元格_后缀=Environment.NewLine;
如果(!lastCell),则为else
单元格\u后缀=“\t”;
}else if(format==DataFormats.CommaSeparatedValue){
如果(lastCell&!inLastRow)
单元格_后缀=Environment.NewLine;
如果(!lastCell),则为else
单元格_后缀=“,”;
}else if(format==DataFormats.Html){
if(inFirstRow&&firstCell)
表_前缀=”;
如果(inLastRow&&lastCell)
表_后缀=”;
if(第一单元)
行_前缀=”;
如果(最后一个单元格)
行_后缀=”;
单元格_前缀=”;
单元格_后缀=”;
如果(!已选定){
value=“”;
}
}否则{
返回值;
}
值=表前缀+行前缀+单元格前缀+值+单元格后缀+行后缀+表后缀;
返回值;
}
这让我相信我应该重写
GetEditedFormattedValue
,并检查
Context
参数中设置的
ClipboardContent
位,允许我检测它是否正在获取剪贴板的值,如果是,则返回ComboBox单元格子体的值,而不是DisplayValue

但是,无法重写GetEditedFormattedValue

因此,我被迫在列中创建一个标志,如果单元格的
GetClipboardContent
正在运行,则将其设置,如果设置了标志,则在网格的
CellFormatting
事件中返回单元格的未格式化值


工作正常。

简短回答:Handle
CellFormatting
,如果在剪贴板“值获取”过程中请求格式化,则返回未格式化的值,而不是显示值。
要能够检测到这一点,让combobox列创建一个combobox单元格子体,当调用其
GetClipboardContent
方法时,该子体在列的属性中设置一个标志

长答覆:

Mh,基本方法似乎是这样做的(来自Mono的c代码):

受保护的虚拟对象GetClipboardContent(int-rowIndex、bool-firstCell、bool-lastCell、bool-inFirstRow、bool-inLastRow、字符串格式){
如果(DataGridView==null)
返回null;
if(rowIndex<0 | | rowIndex>=DataGridView.RowCount)
抛出新ArgumentOutOfRangeException(“rowIndex”,“指定的参数超出了有效值的范围。”);
字符串值=null;
如果(选定){
DataGridViewCellStyle=GetInheritedStyle(null,rowIndex,false);
value=GetEditedFormattedValue(行索引、DataGridViewDataErrorContexts.ClipboardContent | DataGridViewDataErrorContexts.Formatting)作为字符串;
}
如果(值==null)
value=string.Empty;
字符串表\u前缀=string.Empty,单元格\u前缀=string.Empty,行\u前缀=string.Empty;
字符串表_后缀=string.Empty,单元格_后缀=string.Empty,行_后缀=string.Empty;
if(format==DataFormats.unicodext | | format==DataFormats.Text){
    protected virtual object GetClipboardContent (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format) {
            if (DataGridView == null)
            return null;

        if (rowIndex < 0 || rowIndex >= DataGridView.RowCount)
            throw new ArgumentOutOfRangeException ("rowIndex", "Specified argument was out of the range of valid values.");

        string value = null;

        if (Selected) {
            DataGridViewCellStyle style = GetInheritedStyle (null, rowIndex, false);
            value = GetEditedFormattedValue (rowIndex, DataGridViewDataErrorContexts.ClipboardContent | DataGridViewDataErrorContexts.Formatting) as string;
        }

        if (value == null)
            value = string.Empty;

        string table_prefix = string.Empty, cell_prefix = string.Empty, row_prefix = string.Empty;
        string table_suffix = string.Empty, cell_suffix = string.Empty, row_suffix = string.Empty;

        if (format == DataFormats.UnicodeText || format == DataFormats.Text) {
            if (lastCell && !inLastRow)
                cell_suffix = Environment.NewLine;
            else if (!lastCell)
                cell_suffix = "\t";
        } else if (format == DataFormats.CommaSeparatedValue) {
            if (lastCell && !inLastRow)
                cell_suffix = Environment.NewLine;
            else if (!lastCell)
                cell_suffix = ",";
        } else if (format == DataFormats.Html) {
            if (inFirstRow && firstCell)
                table_prefix = "<TABLE>";
            if (inLastRow && lastCell)
                table_suffix = "</TABLE>";
            if (firstCell)
                row_prefix = "<TR>";
            if (lastCell)
                row_suffix = "</TR>";
            cell_prefix = "<TD>";
            cell_suffix = "</TD>";

            if (!Selected) {
                value = "&nbsp;";
            }
        } else {
            return value;
        }

        value = table_prefix + row_prefix + cell_prefix + value + cell_suffix + row_suffix + table_suffix;

        return value;
    }