Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6472059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:17:52+00:00 2026-05-25T06:17:52+00:00

Some of my DataGridViewCells return the wrong value in their GetClipboardContent method. They are

  • 0

Some of my DataGridViewCells return the wrong value in their GetClipboardContent method. They are cells in a DataGridViewComboBoxColumn cells, so they use the displayed property, not the value property of the cell. I want it to return the value itself.

An initial attempt was simply using

  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

in my DataGridViewComboBoxCell descendant but then I noted that this method is called more than one time per cell value, once for every data format DataGridView supports by standard, which are format=”HTML”, “Text”, “UnicodeText” and “Csv”.

For csv, the base implementation appends a comma if it’s not the last cell, for html it adds the correct tags depending on if it’s the first/last row/cell in the table/table row, etc. I consider this format-specific, not cell-value specific.

So how could I replace the value that ends up in the clipboard without re-implementing all those format-specific aspects? That would result in quite some code for functionality that already exists in the base class, wouldn’t it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:17:52+00:00Added an answer on May 25, 2026 at 6:17 am

    Short answer: Handle CellFormatting, and if formatting is requested during a clipboard “value fetch”, return the unformatted value instead of the display value. To be able to detect this, let the combobox column create a combobox cell descendant which sets a flag in the column’s properties when its GetClipboardContent method is called.

    Long answer:

    Mh, it seems the base method does something like this (c# code from Mono from http://www.eg.bucknell.edu/~cs208/subpages/software/src/mono-2.6.7/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCell.cs):

        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;
        }
    

    That lets me believe that I should override GetEditedFormattedValue and check for the ClipboardContent bit being set in the Context argument, allowing me to detect if it is fetching the value for the clipboard, and if so, return the Value of the ComboBox cell descendant, not the DisplayValue.

    GetEditedFormattedValue cannot be overriden, though.

    So i am forced to create a flag in the column, set it if the cell’s GetClipboardContent is running, and return the unformatted value of the cell in the Grid’s CellFormatting event if the flag is set.

    Works fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some time ago a friend of mine told me not to use realloc because
Some virtual machines do not have displayed card at all. I am wondering OpenGL
Some developers are particular about how they format their codes, for some good reasons.
Some consultants built some code for us. I'm looking at a method they wrote
Some programming languages such as Java and C# include encryption packages in their standard
I'm not able to edit my datagridview cells when a number of identical calls
Some controls in Windows Phone 7 have touchable area around, if you touch not
Some kind of problem I can't resolve… In some app, a method called on
Some variants of this question have been asked before, but not this exact one.
I'm working on a grid view that will display cells that will have some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.