codeGEN
codeGEN

Reputation: 714

Cloning datagrid view cell

I have a usercontrol which inherits the winforms datagirdviewcontrol & in here I am trying to clone a cell with its values & store it in a variable. I have cloned the cell with its value by using the below function.

Private Function CloneCellWithValue(ByVal cell As DataGridViewCell) As DataGridViewCell
    Console.WriteLine("Original cell @ row : " & cell.RowIndex & " column : " & cell.ColumnIndex & " value : " & cell.Value)
    CloneCellWithValue = cell.Clone()
    Console.WriteLine("Cloned cell @ row : " & CloneCellWithValue.RowIndex & " column : " & CloneCellWithValue.ColumnIndex & " value : " & CloneCellWithValue.Value)
    CloneCellWithValue.Value = cell.Value
End Function

I pass the cell that needs to be cloned to this function, however the cell doesn't get cloned. I tried to log the properties such as row index, column index and value of the cloned cell to make sure the cell was cloned properly.

And the console displays.

Original cell @ row : 1 column : 1 value : 
Cloned cell @ row : -1 column : -1 value : 

which means the cell hasn't been cloned properly. I need help with cloning the cell with its properties including its value.

Upvotes: 0

Views: 511

Answers (1)

rory.ap
rory.ap

Reputation: 35280

System.Windows.Forms.DataGridViewCell.Clone() creates an exact copy of the cell, but it doesn't mean that that cell will share the same row or column index. It just means that it will have the same format, etc.:

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.clone%28v=vs.110%29.aspx

Note: this will not clone the value of the cell. You will have to copy the value from the source cell after you clone it. Also, no two cells can occupy the same spot in the grid, so it would be impossible for it to have the same row and column index.

Upvotes: 2

Related Questions