ObjectARX
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Merging two cells in a row?

1 REPLY 1
Reply
Message 1 of 2
chikito1990
446 Views, 1 Reply

Merging two cells in a row?

Hi all,

 

I am trying to merge two cells in one but i cannot find proper method to do that. Any suggestions?

 

Example:
I am creating a table with dynamic number of rows and columns: 

 for (int i = 3; i < tbRight.NumRows; i++)
                {
                    for (int j = 0; j < tbRight.NumColumns; j++)
                    {
                        if (num2 == numCircles - 1)
                        {
                            Cell c = new Cell(tbRight, i, j);
                            c.TextHeight = 450;
                            tbRight.SetTextString(i, j, (num2 + 1).ToString() + "# - " + rightConn[num2]);
                            num2++;
                            
                        }
                        else if (num2 > numCircles - 1)
                        {   }
                        else
                        {
                            Cell c = new Cell(tbRight, i, j);
                            c.TextHeight = 450;
                            tbRight.SetTextString(i, j, (num2 + 1).ToString() + "# - " + rightConn[num2]);
                            num2++;
                        }
                    }
                }

 First row of the table is the header and i have two free rows with two columns each. I want to merge all of them to look like one row.

 

 

On the other hand, is it possible to align the text in the table in the middle and also the vertical align to be in the middle.

 

Thank you in advance!

1 REPLY 1
Message 2 of 2

Hi,

 

The following sample illustrates how to merge cells. Also it creates a table style that allows to control the text alignment inside the cells.

 

[CommandMethod("CreateTable")]
public void CreateTable()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    PromptPointOptions ppo = new PromptPointOptions("\nSpecify insertion point: ");

    PromptPointResult ppr = ed.GetPoint(ppo);

    if (ppr.Status != PromptStatus.OK)
        return;

    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        Table table = new Table();
                
        table.Position = ppr.Value;

        table.InsertColumns(1, 2.0, 6);
        table.InsertRows(1, 1.0, 5);

        CellRange cr = CellRange.Create(table, 0, 0, 1, 1);

        table.MergeCells(cr);

        table.Cells[0, 0].Value = "This is cell[0][0]";

        table.Cells[0, 0].State = CellStates.ContentModifiedAfterUpdate;

        string TableStyleName = "MyTableStyle1";

        table.TableStyle = createTableStyle(db, TableStyleName);

        BlockTableRecord btr = Tx.GetObject(
            db.CurrentSpaceId, 
            OpenMode.ForWrite) 
                as BlockTableRecord;

        btr.AppendEntity(table);
        Tx.AddNewlyCreatedDBObject(table, true);
        Tx.Commit();
    }
}

public ObjectId createTableStyle(Database db, string TableStyleName)
{
    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        DBDictionary dico = Tx.GetObject(
            db.TableStyleDictionaryId, 
            OpenMode.ForWrite) as DBDictionary;

        if (dico.Contains(TableStyleName))
        {
            //Already exists
            return dico.GetAt(TableStyleName);
        }

        DbDictionaryEnumerator enumerator = dico.GetEnumerator();
        enumerator.MoveNext();

        //Get first style in the TableStyleDictionary and clone it
        TableStyle style = Tx.GetObject(
            enumerator.Current.Value, 
            OpenMode.ForRead) as TableStyle;

        TableStyle newStyle = style.Clone() as TableStyle;

        ObjectId newStyleId = dico.SetAt(TableStyleName, newStyle);

        newStyle.Name = TableStyleName;
        newStyle.TextHeight(RowType.DataRow);
        newStyle.SetTextHeight(10, 1);
        newStyle.SetAlignment(CellAlignment.MiddleLeft, 1);

        Tx.AddNewlyCreatedDBObject(newStyle, true);
        Tx.Commit();

        return newStyleId;
    }
}

 

Regards,

Philippe.

 



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost