Inserting rows into a C1FlexGrid

Posted by: jacob.a.buchanan6.ctr on 3 December 2025, 6:17 pm EST

  • Posted 3 December 2025, 6:17 pm EST

    The overall goal is when the user has a full row selected and they hit ctrl+v to paste, then it should insert new rows and paste the contents to those new rows. The issue I was having is that if a column is sorted and I insert a new blank row then it automatically moved the row to its sorted position before I have the chance to paste the contents.

    I have a C1FlexGrid showing my class with auto generated columns. I set ClipboardCopyMode to ExcludeHeader and my ClipboardPasteMode to None. I then listen to the KeyDown event on my grid with the intention of doing custom clipboard pasting. In the KeyDown event my code essentially looks like this:

    if (e.Key == Key.V && Keyboard.Modifiers.HasFlat(ModifierKeys.Control) && Clipboard.ContainsText() && flexGrid.Selection.ColumnCount == flexGrid.Columns.Count && flexGrid.ItemsSource is IList<MyClass> myData)
    {
    	string[] rows = Clipboard.GetText()?.Split(new [] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
    	int insertIdx = flexGrid.Selection.Row;
    	for (int i = 0; i < rows.Length; i++)
    	{
    		myData.Insert(insertIdx, new MyClass());
    	}
    	flexGrid.Select(new CellRange(insertIdx, 0, insertIdx + rows.Length, flexGrid.Columns.Count), true);
    }
    flexGrid.Paste();

    The idea being that if a full row is selected, insert a new row for each line of text on the clipboard, select those new rows, then use flexGrid.Paste() to handle the assigning of values to MyClass.

    MyClass has a DateTime field. If I sort or filter this column, then when I insert ‘new MyClass()’ the new DateTime is null and gets filtered out/sorted to the bottom. Then when I try pasting the data in it paste it to the wrong location overwriting my existing rows and leaving behind blank rows.

    I tried using flexGrid.GetRowIndex(insertIdx) to get the associated grid index for the item i just inserted into my source collection, but this always returns the same insertIdx and now the sorted index(row 0). I also tried using flexGrid.SetClipString() instead of just relying on flexGrid.Paste() to insert my data, but this resulted in the same issue.

    Any ideas/tips/tricks would be appreciated! I might be way overcomplicating this whole thing.

  • Posted 6 December 2025, 12:20 am EST

    Hi Jacob,

    Thank you for the detailed information.

    We could replicate the behavior on our end. The behavior you’re seeing happens because FlexGrid immediately re-applies sorting or filtering when you insert a new blank item. Since the new item has a null DateTime value, it gets moved to a different position before the paste happens, which causes the data to paste into the wrong rows.

    A reliable way to fix this is to temporarily remove the grid’s sort descriptions before inserting the new rows, perform the paste, and then restore the sort. This keeps the inserted rows in the correct position during the paste operation.

    Therefore, we suggest you consider the following approach:

    1. Save and clear the current SortDescriptions
    2. Insert the required number of new items into the ItemsSource
    3. Select those items and call flexGrid.Paste()
    4. Restore the saved sorting

    This ensures the paste operation always targets the newly inserted rows and prevents rows from shifting unexpectedly.

    We have attached a sample application for your reference. See FG_InsertRow.zip

    Thanks & regards,

    Aastha

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels