We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Excel Ctrl+D and Ctrl+Enter

Hi, I am using a databound grid. Excel provides a "fill" approach. Ctrl-Enter works on a selected range, if you type in the first cell and press Ctrl-Enter that cell is copied to the selected cells. Ctrl-D does a fill down. How do I do this in a GridDataboundGrid? Thanks

11 Replies

AD Administrator Syncfusion Team March 15, 2004 06:25 PM UTC

I think you can do this type of thing handling CurrentCellKeyDown. Here is code that tries to handle the ctl+Enter.
private void gridDataBoundGrid1_CurrentCellKeyDown(object sender, KeyEventArgs e)
{
	bool ctl = ((Control.ModifierKeys & Keys.Control) != 0);
	if(ctl && e.KeyCode == Keys.Enter)
	{
		GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
		string newValue = cc.Renderer.ControlText;
		GridRangeInfo range = this.gridDataBoundGrid1.Selections.Ranges.ActiveRange;
		if(range.Height > 1 || range.Width > 1)
		{
			this.gridDataBoundGrid1.Model.ChangeCells(range, newValue);
			e.Handled = true;
		}
	}
}


DF Doug Finke March 15, 2004 10:16 PM UTC

Thanks... Two more questions. If the target column is a double, how do you convert? Is there a way to support selecting non-contiguous cells? For example cell A1 B3 C5.


AD Administrator Syncfusion Team March 15, 2004 10:46 PM UTC

The same code worked for me for a DataColumn whose type was double. What problem did you run into? Default selection support includes selecting multiple ranges using ctl key or shift key to select another range while maintaining the existing selections.


DF Doug Finke March 16, 2004 12:08 PM UTC

My apologies. I misspoke. It does convert properly. If I select a numeric column and alpha column, then type in a character. An error is throw, where can I catch that and display a MessageBox? Thanks Clay >The same code worked for me for a DataColumn whose type was double. What problem did you run into? > >Default selection support includes selecting multiple ranges using ctl key or shift key to select another range while maintaining the existing selections.


AD Administrator Syncfusion Team March 16, 2004 01:35 PM UTC

Normally, you can catch these validation problems in a GridDataBoundGird in the ValidateFailed event. You can just set the message if you would like, or you can display your own dialog, and then set e.Handled = true to tell the grid it does not need to do anything. private void gridDataBoundGrid1_ValidateFailed(object sender, GridValidateFailedEventArgs e) { GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell; cc.ErrorMessage = "MyMessage"; }


AD Administrator Syncfusion Team March 16, 2004 02:30 PM UTC

The below works if I edit a single cell. It doesn''t work withteh Ctrl-Enter. I suspect it is because it uses this.gridDataBoundGrid1.Model.ChangeCells and bypasses the Grid. >Normally, you can catch these validation problems in a GridDataBoundGird in the ValidateFailed event. You can just set the message if you would like, or you can display your own dialog, and then set e.Handled = true to tell the grid it does not need to do anything.


AD Administrator Syncfusion Team March 16, 2004 03:03 PM UTC

You do not have to use grid.ChangeCells(range, newValue) to set the values in keydown. You can loop through the cells and set them one at the time, checking them before-hand that it is a valid value to put into the cell. You can do whatever validation you need before you set the value into the cell. this would add more work as the ChanegCells call handles this looping process for you. But, if you need to do cell by cell validation, then doing teh looping yourself is an option. Another option is to put the ChangeCells in a try-catch, and cancel the action if things are bad. In a GridDataBoundGrid, every cell in teh column is of teh same type. So, once you have a bad value to be put into the cell, it will continue to be a bad value for every cell in that column. Here is some code.
try
{
	this.gridDataBoundGrid1.Model.ChangeCells(range, newValue);
}
catch
{
	cc.Lock();
	 MessageBox.Show("Cannot complete...");
	cc.Unlock();
	 cc.RejectChanges();
        this.gridDataBoundGrid1.Binder.CancelEdit();
}
e.Handled = true;


DF Doug Finke March 17, 2004 11:33 AM UTC

Thanks Clay. Do you have a snippet that shows how to set them one at a time? >You do not have to use grid.ChangeCells(range, newValue) to set the values in keydown. You can loop through the cells and set them one at the time, checking them before-hand that it is a valid value to put into the cell. You can do whatever validation you need before you set the value into the cell. this would add more work as the ChanegCells call handles this looping process for you. But, if you need to do cell by cell validation, then doing teh looping yourself is an option. > >Another option is to put the ChangeCells in a try-catch, and cancel the action if things are bad. In a GridDataBoundGrid, every cell in teh column is of teh same type. So, once you have a bad value to be put into the cell, it will continue to be a bad value for every cell in that column. Here is some code. >
>try
>{
>	this.gridDataBoundGrid1.Model.ChangeCells(range, newValue);
>}
>catch
>{
>	cc.Lock();
>	 MessageBox.Show("Cannot complete...");
>	cc.Unlock();
>	 cc.RejectChanges();
>        this.gridDataBoundGrid1.Binder.CancelEdit();
>}
>e.Handled = true;
>


AD Administrator Syncfusion Team March 17, 2004 04:43 PM UTC

Assuming range is a Cells range, you can try code like: for(int row = range.top; row <= range.Bottom; ++row) { for(int col = range.Left; col <= range.Right; ++col) { this.gridDataBoundGrid1[row, col].Text = newValue; } }


AH Adam Hwang April 21, 2010 04:27 PM UTC

Is there an updated example of this functionality? Neither of these solutions worked for our grid version 8.1.0.

Neither throw errors but the values wont change. I've tried changing both the cell's Text and CellValue. My last incarnation of the Fill Down functionality that does not work:


private void gridDataBoundGrid1_CurrentCellKeyDown(object sender, KeyEventArgs e)
{
bool ctl = ((Control.ModifierKeys & Keys.Control) != 0);
if (ctl && e.KeyCode == Keys.D)
{
GridRangeInfo range = gridDataBoundGrid1.Selections.Ranges.ActiveRange;

for (int row = range.Top; row <= range.Bottom; ++row)
{
for (int col = range.Left; col <= range.Right; ++col)
{
gridDataBoundGrid1[row, col].CellValue = gridDataBoundGrid1[range.Top, col].CellValue;
}
}
}
}


JJ Jisha Joy Syncfusion Team April 23, 2010 11:34 AM UTC

Hi Adam,

I have prepared a sample to test the issue. it is working fine in version 8.1 too. See the sample attached.


Please let me know if you have any questions.

Regards,
Jisha



GDBG_77677f7b.zip

Loader.
Up arrow icon