2
votes

I am attempting to use the automatic header text enumeration value to copy data from my datagridview. I want to be able to select a single cell without column headers. I want to be able to select a full row or all cells in the grid and include column headers.

msdn says The text values of selected cells can be copied to the Clipboard. Row or column header text is included for rows or columns that contain selected cells only when the DataGridView.SelectionMode property is set to RowHeaderSelect or ColumnHeaderSelect and at least one header is selected.

Using that information I set the datagridview as follows: rowheadersvisible = true, multiselect = true, selectionmode=rowheaderselect, clipboardcopymode = enablewithautoheadertext

In this configuration I get no column header information despite whether I select one cell, one row, or all cells. What else must be done to get the results described in the MSDN description?

As a workaround, I handled the keydown event and am able to manipulate the grid to meet my needs at runtime. I would prefer to learn from someone what I am not doing correctly with the EnableWithAutoHeaderText Enumeration value instead of keeping this code.

Private Sub dgvScanHistory_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvScanHistory.KeyDown
    If e.Modifiers = Keys.Control Then
        ctrlPressed = True
    End If
    If e.KeyCode = Keys.C And ctrlPressed Then
        'this is a hack because autoinclude header cells isn't working correctly.
        If dgvScanHistory.AreAllCellsSelected(True) = True Then
            dgvScanHistory.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
        Else
            dgvScanHistory.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText

        End If
        Clipboard.SetDataObject(dgvScanHistory.GetClipboardContent())
        ctrlPressed = False
    End If
End Sub
1
Problem reproduced with 4.0. You might want to remove I want to be able to select a single cell without column headers as it conflicts with I want to be able to select a full row or all cells in the grid and include column headers.. - Bjørn-Roger Kringsjå
I do not believe it conflicts. I need it this way for a specific purpose. I am selecting the row header or using the top left cell to select all cells so that I can paste into excel for research. The single cell is used to copy a value so I can use that value on another screen of my application to look up information. - TWood

1 Answers

0
votes

Put your code in the SelectionChanged event. I needed to do something similar where selecting a single cell or multiple cells in the same column would copy without the header, but selecting any group of cells spanning more than one column would include the headers. Like you, I couldn't get EnableWithAutoHeaderText to do anything like you would think from reading MS's doc. Also, I wanted to use the header click for sorting so it didn't seem like ColumnHeaderSelect was going to work or me anyway.

private void dgvScanHistory_SelectionChanged(object sender, EventArgs e)
{
  int cellsCnt = 0;
  colCnt = dgvScanHistory.SelectedCells.Cast<DataGridViewCell>().Select(c => c.ColumnIndex).Distinct().Count();
  if (colCnt > 1)
  {
    dgvScanHistory.ClipboardCopyMode =
      DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  }
  else
  {
    dgvScanHistory.ClipboardCopyMode =
      DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
  }
}

Using this code, the ClipboardCopyMode property will get a lot of 'exercise', but the app doesn't seem to mind.