Hi there.
I am having an annoying problem with a DataGridView not linked to any database; I save/load to/from a simple XML file.
(Using VSNet 2005release version- developping in VB.NET)
When editing a cell's content, highlighting a portion of text and pressing CTRL-C, the complete cell content is copied to the clipboard.
Is there an easy way to only have the selected text copied on CTRL-C? I know that I can achieve this by right-clicking on the selection and selecting copy, but for speed, I would like to use the CTRL-C method.
Also, why is that when I press SHIFT-SPACE, the complete line is selected?
Thanks for the help,
Zolt |
| Zolt Thursday, March 23, 2006 3:27 PM |
Hello,
I couldn't find a way to override default behaviour of the grid but my solution is like that:
protected override void OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
{
//Ensure that none of column ordering is in automatic mode
foreach (DataGridViewColumn column in this.Columns)
column.SortMode = DataGridViewColumnSortMode.Programmatic;
this.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
this.Columns[e.ColumnIndex].Selected = true;
base.OnColumnHeaderMouseClick(e);
}
protected override void OnRowHeaderMouseClick(DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Select whole row.
this.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
this.Rows[e.RowIndex].Selected = true;
}
base.OnRowHeaderMouseClick(e);
}
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
{
// While editing a cell, disallowrow or column selectto avoid Shift+Space row selection default behaviour.
this.SelectionMode = DataGridViewSelectionMode.CellSelect;
base.OnCellBeginEdit(e);
}
If you disallow row or column selection while editing a cell, the Shift-Space event does not rise, which is exactly what I needed. Not sure if that would fit your needs though.
|
| krejcimar Friday, September 01, 2006 7:51 AM |
I can't help but I would like to ask if the problem of pressing shift-space was solved. When I press shift-space, I wish just space was passed to the EditingControl of the DataGridView and no row was selected. |
| krejcimar Tuesday, August 29, 2006 9:38 PM |
Would you provide more information such as what properties of datagridview you modified, what events you have added handlers to, what you used is datagridviewtextboxcolumn? |
| Wang Chi Wednesday, August 30, 2006 8:46 AM |
No, the shift-space problem was not resolved. If you find anything on your side, please let me know! |
| Zolt Wednesday, August 30, 2006 12:20 PM |
The datagrid (DataGridView) I use is bound to a recordset.
I do not manually generate the columns: they are taken care by the object, so I am not modifying any event, nor did I add any handlers to it.
The only 2 things I added are to be able to track if there was any changes between edit and save: I keep the old value before editing and compare it with the new one after leaving the cell.
Here are the additions I made :
Private Sub DataGrid1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGrid1.CellEnter
sCellValue = DataGrid1.CurrentCell.Value.ToString
End Sub
Private Sub DataGrid1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGrid1.CellEndEdit
If DataGrid1.CurrentCell.Value.ToString <> sCellValue Then
If e.ColumnIndex = 0 Then
''Only for first column, put the text to lower case, and replace spaces with underscores (_)
DataGrid1.CurrentCell.Value = DataGrid1.CurrentCell.Value.ToString.Trim.ToLower.Replace( " ", "_")
Me.chkDirty.Checked = True
End If
End If
End Sub
Any ideas? :) |
| Zolt Wednesday, August 30, 2006 12:22 PM |
Regarding the shift-space problem, I tried everything that I could think of, including searching the whole web but found nothing. :-(
I found the problem pretty annoying... |
| krejcimar Wednesday, August 30, 2006 12:26 PM |
It is pretty annoying indeed.
I have switched to another project since then, but I keep this in my list of things to work on.
If I come up with something at some point, I will let you know. Please do the same if you find a solution :) |
| Zolt Wednesday, August 30, 2006 12:29 PM |
OK, I would inform you. |
| krejcimar Wednesday, August 30, 2006 12:35 PM |
Hello,
I couldn't find a way to override default behaviour of the grid but my solution is like that:
protected override void OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
{
//Ensure that none of column ordering is in automatic mode
foreach (DataGridViewColumn column in this.Columns)
column.SortMode = DataGridViewColumnSortMode.Programmatic;
this.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
this.Columns[e.ColumnIndex].Selected = true;
base.OnColumnHeaderMouseClick(e);
}
protected override void OnRowHeaderMouseClick(DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Select whole row.
this.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
this.Rows[e.RowIndex].Selected = true;
}
base.OnRowHeaderMouseClick(e);
}
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
{
// While editing a cell, disallowrow or column selectto avoid Shift+Space row selection default behaviour.
this.SelectionMode = DataGridViewSelectionMode.CellSelect;
base.OnCellBeginEdit(e);
}
If you disallow row or column selection while editing a cell, the Shift-Space event does not rise, which is exactly what I needed. Not sure if that would fit your needs though.
|
| krejcimar Friday, September 01, 2006 7:51 AM |
Can not reproduce your problems here:-(. You may repeat the steps what you have done, and check this behavior at the end of each step, find out what causes this issue. |
| Wang Chi Friday, September 01, 2006 9:00 AM |
Try this: dataGridView.MultiSelect = false; dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect; |
| nshafaee Wednesday, June 04, 2008 9:53 PM |
Hi,
Try this to solve the whole cell being copied problem.
public class CustomDataGridView : DataGridView { public override System.Windows.Forms.DataObject GetClipboardContent() { if (this.SelectedCells.Count == 1) { System.Windows.Forms.DataObject dataObject = new System.Windows.Forms.DataObject(); if (this.CurrentCell.IsInEditMode) { if (this.EditingControl is System.Windows.Forms.TextBox) { string text = ((System.Windows.Forms.TextBox)this.EditingControl).SelectedText; dataObject.SetText(text); } } else { dataObject.SetText(this.CurrentCell.Value.ToString()); } return dataObject; } else { return base.GetClipboardContent(); } } } |
| MSearles Monday, November 03, 2008 12:26 AM |
Solution above does not restore row selection mode when header row is clicked, which is not trivial. The solution I found is to handleOnCellMouseEnter event and set the grid selection mode accordingly.
protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
{
//this enables using Ctrl-C and other shortcuts to copy cell values.
this.SelectionMode = e.ColumnIndex == -1 ? DataGridViewSelectionMode.FullRowSelect : DataGridViewSelectionMode.CellSelect;
base.OnCellMouseEnter(e);
}
|
| glevik Thursday, September 24, 2009 10:42 PM |