How to insert column at index in DataGrid


#1

Hi,

i would like to insert a column at a specific index in an already populated DataGrid.

How can i achieve this?

Thanks,

Davide


#2

Greetings Program!

From the documentation, it doesn’t look like you can add a column at a specific index but a possible solution would be to instantiate the columns and add them to an array then add the columns from the array to the grid. When you want to add a column, add it to the array and replace the datagrid instance with a new one.

I don’t know if this is the best way or the most efficient but perhaps something like:

@implementation ViewController
{
    ShinobiDataGrid* _shinobiDataGrid;
    NSMutableArray *columns;
}

- (void)viewDidLoad
{
   [code]
    
    columns = [[NSMutableArray alloc] init];
	
    // add a name column
    SDataGridColumn* nameColumn = [[SDataGridColumn alloc] initWithTitle:@"Name"];
    nameColumn.width = @584;
	[columns insertObject:nameColumn atIndex:0];
    
    // add an age column
    SDataGridColumn* ageColumn = [[SDataGridColumn alloc] initWithTitle:@"Age"];
    ageColumn.width = @600;
    [columns insertObject:ageColumn atIndex:1];

    // Add columns from array. 
    [_shinobiDataGrid addColumnsFromArray:(NSArray *)columns];

    [more code]
}

-(void)insertColumn
{
     // Insert "Last Name" column between Name and Age columns.
    SDataGridColumn* lastNameColumn = [[SDataGridColumn alloc] initWithTitle:@"Last Name"];
    lastNameColumn.width = @300;
    [columns insertObject:lastNameColumn atIndex:1];

    // You can either remove the columns from the datagrid first or remove/replace the instance with a new instance.
    // [_shinobiDataGrid removeColumnAtIndex:0]; // loop through for the number of existing columns if you do this.
    [_shinobiDataGrid addColumnsFromArray:(NSArray *)columns];
    [_shinobiDataGrid reload];
}

Wg