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

Mikael9 reputationPosted Jul 20, 2006, 12:05 PM EDT
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();
....
Guha Prabakaran9 reputationPosted Jul 20, 2006, 12:29 PM EDT
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();}
}
Mikael9 reputationPosted Jul 20, 2006, 12:43 PM EDT
Just a small fix needed :)
DataGridViewCell[i, j] = new DataGridViewCell();
to
cells[i, j] = new DataGridViewCell();
Guha Prabakaran9 reputationPosted Jul 20, 2006, 12:48 PM EDT
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
Mikael9 reputationPosted Jul 20, 2006, 12:52 PM EDT
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 Prabakaran9 reputationPosted Jul 20, 2006, 12:56 PM EDT
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
Mikael9 reputationPosted Jul 20, 2006, 1:09 PM EDT
What object holds the data that you want to copy and what type is the object?
Guha Prabakaran9 reputationPosted Jul 20, 2006, 1:23 PM EDT
public
static DataGridViewSelectedCellCollection CopyDataGridViewCells(DataGridView dgv, bool alsoCopyToClipBoard){
DataGridViewSelectedCellCollection selectedCells = dgv.SelectedCells; if (alsoCopyToClipBoard){
DataGridViewCell[,] cells = SortCells(selectedCells);CopyToClipboard(cells);
}
return selectedCells;}