views:

25

answers:

1

Hello,

I have a table with 3 columns. Now I want to insert another column, but I want to insert the new column after the first one. Now I'm wondering how this can be done. If I'm not wrong, the Table.Columns property isn't really representing columns, it's more representing their layout but has nothing to do with the values of the columns? Look e.g. at http://msdn.microsoft.com/en-us/library/system.windows.documents.table.columns.aspx

The TableColumn objects returned by this property can, in conjunction with the TableCell objects in the column, be used to define layout of columns but they do not determine the actual number of columns rendered. It is the TableCell objects in a table that determine how many columns are actually rendered. For example, if you define 3 columns but only have table cells for 2 columns, only 2 columns will be rendered.

So I guess, that if I want to add a new column with values, I have to loop through the rows of the table and in every row I have to add a new cell at the right position?

A: 

Your guess is correct, except that there are some additional complications to consider.

If any of the cells to the left of the insert position have a RowSpan or ColumnSpan greater than 1, you will need to make corresponding adjustments to the cell indexes at which you insert new cells. For example, to insert a new column at the third visual column position, you would theoretically insert new cells before the existing cells with index 2. However, if any of the rows has a cell at index 0 with RowSpan=2, this cell will span two columns, so the new cell in this row must be inserted before the cell with index 1.

If a column-spanning cell crosses the column position that you are inserting into, you do not need to insert a new cell - just increase the the ColumnSpan of the existing cell. Similarly if a previous row has a row-spanning cell that falls in one of the columns to the left of your insertion position, you will need to make adjustments for this.

In fact, if you want to support the full range of column insert scenarios, the code will become quite complex. You might want to model your solution on the private framework method TextRangeEditTables.InsertColumn().

Tim Coulter