All,
When it hits the Bold line, I get the error, Index was outside the bounds of the array.
Anyone know what I need to know.
Thanks
Guha
<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>
DataGridViewCell[,] cells = new DataGridViewCell[15, 1];
foreach (DataGridViewCell cell in selectedCells)
{
cells[0, 1].Value = cell.value;
}
When it hits the Bold line, I get the error, Index was outside the bounds of the array.
Anyone know what I need to know.
Thanks
Guha
<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>
DataGridViewCell[,] cells = new DataGridViewCell[15, 1];
foreach (DataGridViewCell cell in selectedCells)
{
cells[0, 1].Value = cell.value;
}
Guha PrabakaranPosted Jul 20, 2006, 5:23 PM
public
static DataGridViewSelectedCellCollection CopyDataGridViewCells(DataGridView dgv, bool alsoCopyToClipBoard){
DataGridViewSelectedCellCollection selectedCells = dgv.SelectedCells; if (alsoCopyToClipBoard){
DataGridViewCell[,] cells = SortCells(selectedCells);CopyToClipboard(cells);
}
return selectedCells;}
MikaelPosted Jul 20, 2006, 5:09 PM
What object holds the data that you want to copy and what type is the object?
Guha PrabakaranPosted Jul 20, 2006, 4:56 PM
Yes, What I am doing is copying the cells I have selected to be pasted later. So the class that I got for another gentleman, which I am using is defining an instance of DataGridViewCell and populating this with the data from the selected cells.
Would you know of an alternate way for me to be able to copy and paste into a DataGridView?
I do not see a way to attach the class, if not I can attach it.
Thanks
Guha
MikaelPosted Jul 20, 2006, 4:52 PM
If it's only to go through the cells you've selected or fetched just use:
DataGridViewSelectedCellCollection cellCollection = dataGridView.SelectedCells;
...
Then use the functions that cellCollection offer to traverse the cells, it should be compatible with foreach (DataGridViewCell cell in cellCollection) ...
If you want to create a local copy then the collection has a function called CopyTo that you can use like this:
DataGridViewCell[] cells = new DataGridViewCell[cellCollection.Count];
cellCollection.SelectedCells.CopyTo(cells, 0);
Guha PrabakaranPosted Jul 20, 2006, 4:48 PM
I still get
Error 3 Cannot create an instance of the abstract class or interface
when I do
DataGridViewCell
[,] cells = new DataGridViewCell[colCount - 1, rowCount - 1]; int i; for (i = 0; i < colCount; i++){
int j; for (j = 0; j < rowCount; j++){
new DataGridViewCell();}
}
Thanks again.
Guha
MikaelPosted Jul 20, 2006, 4:43 PM
Just a small fix needed :)
DataGridViewCell[i, j] = new DataGridViewCell();
to
cells[i, j] = new DataGridViewCell();
Guha PrabakaranPosted Jul 20, 2006, 4:29 PM
Mikael,
I tried this and I get compile errors
Error 3 'System.Windows.Forms.DataGridViewCell' is a 'type' but is used like a 'variable'
Error 4 Cannot create an instance of the abstract class or interface
DataGridViewCell
[,] cells = new DataGridViewCell[colCount - 1, rowCount - 1]; int i; for (i = 0; i < colCount; i++){
int j; for (j = 0; j < rowCount; j++){
DataGridViewCell[i, j] = new DataGridViewCell();}
}
MikaelPosted Jul 20, 2006, 4:05 PM
DataGridViewCell[,] cells = new DataGridViewCell[15, 1];
// You're entering the frame and grabbing at something that's not there
foreach (DataGridViewCell cell in selectedCells)
{
cells[0, 1].Value = cell.value;
}
// You have to create the cells before assigning them values
cells[0, 0] = new DataGridViewCell();
cells[0, 1] = new DataGridViewCell();
....