Home Forums WinForms controls Xceed Grid for WinForms Insert Row Reply To: Insert Row

User (Old forums)
Member
Post count: 23064

To insert row by any specific index, I was thinking to add new row as template.
like below ( drag & drop in Xceed Grid):
There is two rows which are from row and target row.
This way is the best choice to me.
Any other ideas? Please let me know.

private void Drag_Drop( object sender, DragEventArgs e )
{

Xceed.Grid.DataRow targetRow= ((Xceed.Grid.Cell)sender).ParentRow as Xceed.Grid.DataRow;
int targetIndex = this.gridControl1.DataRows.IndexOf(targetRow);
this.gridControl1.CurrentRow = this.gridControl1.DataRows[targetIndex];

int fromRowIndex = this.index; // member variable index represents from row’s index

if( (this.index – targetIndex) > 0 ) //from row index is greater than target row index
{
for ( int i = 0 ; i < this.index – targetIndex ; i ++)
{
Xceed.Grid.DataRow temp = this.gridControl1.DataRows.AddNew();
temp.Cells[0].Value = this.gridControl1.DataRows[fromRowIndex].Cells[0].Value;
temp.Cells[1].Value = this.gridControl1.DataRows[fromRowIndex].Cells[1].Value;
temp.Cells[2].Value = this.gridControl1.DataRows[fromRowIndex].Cells[2].Value;

this.gridControl1.DataRows[fromRowIndex].Cells[0].Value= this.gridControl1.DataRows[fromRowIndex – 1].Cells[0].Value;
this.gridControl1.DataRows[fromRowIndex].Cells[1].Value=this.gridControl1.DataRows[fromRowIndex-1].Cells[0].Value;
this.gridControl1.DataRows[fromRowIndex].Cells[2].Value=this.gridControl1.DataRows[fromRowIndex-1].Cells[0].Value;

this.gridControl1.DataRows[fromRowIndex-1].Cells[0].Value= temp.Cells[0].Value;
this.gridControl1.DataRows[fromRowIndex1].Cells[1].Value=temp.Cells[1].Value;
this.gridControl1.DataRows[fromRowIndex-1].Cells[2].Value=temp.Cells[2].Value;
temp.Remove();
index_count –;

}

}
else
{
for ( int i = 0 ; i < targetIndex – this.index ; i ++)
{
Xceed.Grid.DataRow temp = this.gridControl1.DataRows.AddNew();
temp.Cells[0].Value = this.gridControl1.DataRows[fromRowIndex].Cells[0].Value;
temp.Cells[1].Value = this.gridControl1.DataRows[fromRowIndex].Cells[1].Value;
temp.Cells[2].Value = this.gridControl1.DataRows[fromRowIndex].Cells[2].Value;

this.gridControl1.DataRows[fromRowIndex].Cells[0].Value= this.gridControl1.DataRows[fromRowIndex+1].Cells[0].Value;
this.gridControl1.DataRows[fromRowIndex].Cells[1].Value=this.gridControl1.DataRows[fromRowIndex+1].Cells[0].Value;
this.gridControl1.DataRows[fromRowIndex ].Cells[2].Value=this.gridControl1.DataRows[fromRowIndex+1].Cells[0].Value;

this.gridControl1.DataRows[fromRowIndex + 1].Cells[0].Value= temp.Cells[0].Value;
this.gridControl1.DataRows[fromRowIndex+1].Cells[1].Value=temp.Cells[1].Value;
this.gridControl1.DataRows[fromRowIndex+1].Cells[2].Value=temp.Cells[2].Value;
temp.Remove();
index_count ++;
}

}

}

Imported from legacy forums. Posted by JKim (had 705 views)