locked
GridColumn Width not working for me RRS feed

  • Question

  • I am doing in DocumentFormat.OpenXml.WordProcessing the following to get a table with two columns, the first one 2" wide and the second one 4" wide:

      TableGrid tabGrid = new TableGrid(new GridColumn() { Width = "2880" }, new GridColumn() { Width = "5760" });
      table.AppendChild(tabGrid);
    

    I have also set the table width to 8640 dxa using TableProperties.

    The Xml appears to come out correctly:

    <w:body>
      <w:tbl>
        <w:tblPr>
          <w:tblStyle w:val="TableGrid" />
          <w:tblW w:w="8640" w:type="dxa" />
        </w:tblPr>
        <w:tblGrid>
          <w:gridCol w:w="2880" />
          <w:gridCol w:w="5760" />
        </w:tblGrid>
     ...

    When I open the document in Word and go to table properties, both columns do not have Preferred width checked. The Table width is checked and has a value of 6" (8640dxa).

    THe two columns do not maintain the widths of 2" and 4" respectively.  They will just collapse to something just wide enough to hold the contents.  If there is nothing in the column, it will collapse to zero width.

    What am I missing here?  Thanks.

    Saturday, February 11, 2012 4:24 PM

Answers

  • Hi K.Kong,

    Adding to Calvin’s explanation, there are three kinds of
    Table layout properties. This isn’t dependent on the width of the parent cell’s
    width.  Through the UI, when you draw a
    table, then click to see its properties. The drop-down list shows an “AutoFit”
    entry which shows ‘Autofit to content’, ‘Autofit to Window’ and ‘ Fixed Column Width’ 

    Through the UI insert a 2x2 table in a blank Word Docx document. Drag the vertical width bar to set the width of Column 1 to a narrow setting. Right-click the Properties button just outside the top-left corner of the table and seledt the 'Fixed Column Width option. Column 1 conforms to the set column width without regard to the contents of row
    1, column 1. You can test that by inserting lengthy text into Row 2, Column 1. The text in the cell auto wraps to
    the column width.  This is without regards to the contents of the parent cell in Row 1, Column 1.

    The XML for this table similar to the one you built as a demonstration by following the procedure above is shown in the snippet below. At the top of the XML from a table of this 'type' you can see
    in the XML  <w:tblPr>  node , <w:tblLayout w:type=”fixed”/> item.


    	
    <w:tbl>
          <w:tblPr>
            <w:tblStyle w:val="TableGrid"/>
            <w:tblW w:w="0" w:type="auto"/>
            <w:tblLayout w:type="fixed"/>
            <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
          </w:tblPr>
          <w:tblGrid>
            <w:gridCol w:w="2178"/>
            <w:gridCol w:w="7398"/>
          </w:tblGrid>
          <w:tr w:rsidR="00C510CC" w:rsidTr="00C510CC">
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="2178" w:type="dxa"/>
              </w:tcPr>
              <w:p w:rsidR="00C510CC" w:rsidRDefault="00C510CC">
                <w:r>
                  <w:t>On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document.</w:t>
                </w:r>
              </w:p>
              <w:p w:rsidR="00C510CC" w:rsidRDefault="00C510CC"/>
              <w:p w:rsidR="00C510CC" w:rsidRDefault="00C510CC"/>
            </w:tc>
    	
    



    Please remember to click the “Mark as answer” if this
    explanation resolves your dilemma.

    Regards,

    Chris Jensen

    Senior Technical Support Lead


    Chris Jensen

    • Proposed as answer by cjatms Friday, February 24, 2012 3:21 PM
    • Marked as answer by Forrest Guo Sunday, February 26, 2012 5:48 AM
    Wednesday, February 15, 2012 3:32 PM

All replies

  • Hi,

    You should not only change the width of a certain column but also change the width of the cells in that column, see the code below:

            public void OpenDocument(string fileName)
            {
                WordprocessingDocument wordDocument = WordprocessingDocument.Open(fileName, true);
                MainDocumentPart mainDocument = wordDocument.MainDocumentPart;
                Document document = mainDocument.Document;
                Body body = document.Body;
                Table table = body.Descendants<Table>().First();
    
                //change the width of the whole table
                TableWidth tableWidth = table.Descendants<TableWidth>().First();
                tableWidth.Width = "9000";
                //remember change this property, or Word would automatically determined width
                tableWidth.Type = TableWidthUnitValues.Dxa;
    
                TableRow tableRow = null;
                TableCell tableCell = null;
    
                //change the first column width and widths of cells in this column
                GridColumn gridcolumn1 = table.Descendants<GridColumn>().ElementAt(0);
                gridcolumn1.Width = "2000";    
                for (int i = 0; i < table.Elements<TableRow>().Count(); i++)
                {
                    tableRow = table.Elements<TableRow>().ElementAt(i);
                    tableCell = tableRow.Elements<TableCell>().ElementAt(0);
                    tableCell.GetFirstChild<TableCellProperties>().GetFirstChild<TableCellWidth>().Width = "2000";
                }
    
                //second column
                GridColumn gridcolumn2 = table.Descendants<GridColumn>().ElementAt(1);
                gridcolumn1.Width = "6000";  
                for (int i = 0; i < table.Elements<TableRow>().Count(); i++)
                {
                    tableRow = table.Elements<TableRow>().ElementAt(i);
                    tableCell = tableRow.Elements<TableCell>().ElementAt(1);
                    tableCell.GetFirstChild<TableCellProperties>().GetFirstChild<TableCellWidth>().Width = "6000";
                }
    
                //third column
                GridColumn gridcolumn3 = table.Descendants<GridColumn>().ElementAt(2);
                gridcolumn1.Width = "1000";
                for (int i = 0; i < table.Elements<TableRow>().Count(); i++)
                {
                    tableRow = table.Elements<TableRow>().ElementAt(i);
                    tableCell = tableRow.Elements<TableCell>().ElementAt(2);
                    tableCell.GetFirstChild<TableCellProperties>().GetFirstChild<TableCellWidth>().Width = "1000";
                }
    
                wordDocument.MainDocumentPart.Document.Save();
                wordDocument.Close(); 
    
            }
    I hope this helps.

    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 13, 2012 8:48 AM
  • Calvin, thanks. 

    I was under the impression that a column will render to its own width unless a child cell has a width that is larger than the column's explicit width.  If a column will just follow the cells' widths, then what is the purpose of having a width property for the column?

    My column width now behaves to what I need if I the cell width explictly.  And Table Properties in Word shows that the column has a Preferred width set when my cell has an explicit width.  It is confusing, or perhaps wrong, if the column's Preferred width attribute is set not by the column's properties but by a cell's (BTW which cell?) properties.

    Tuesday, February 14, 2012 1:36 AM
  • Hi K.Kong,

    Adding to Calvin’s explanation, there are three kinds of
    Table layout properties. This isn’t dependent on the width of the parent cell’s
    width.  Through the UI, when you draw a
    table, then click to see its properties. The drop-down list shows an “AutoFit”
    entry which shows ‘Autofit to content’, ‘Autofit to Window’ and ‘ Fixed Column Width’ 

    Through the UI insert a 2x2 table in a blank Word Docx document. Drag the vertical width bar to set the width of Column 1 to a narrow setting. Right-click the Properties button just outside the top-left corner of the table and seledt the 'Fixed Column Width option. Column 1 conforms to the set column width without regard to the contents of row
    1, column 1. You can test that by inserting lengthy text into Row 2, Column 1. The text in the cell auto wraps to
    the column width.  This is without regards to the contents of the parent cell in Row 1, Column 1.

    The XML for this table similar to the one you built as a demonstration by following the procedure above is shown in the snippet below. At the top of the XML from a table of this 'type' you can see
    in the XML  <w:tblPr>  node , <w:tblLayout w:type=”fixed”/> item.


    	
    <w:tbl>
          <w:tblPr>
            <w:tblStyle w:val="TableGrid"/>
            <w:tblW w:w="0" w:type="auto"/>
            <w:tblLayout w:type="fixed"/>
            <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
          </w:tblPr>
          <w:tblGrid>
            <w:gridCol w:w="2178"/>
            <w:gridCol w:w="7398"/>
          </w:tblGrid>
          <w:tr w:rsidR="00C510CC" w:rsidTr="00C510CC">
            <w:tc>
              <w:tcPr>
                <w:tcW w:w="2178" w:type="dxa"/>
              </w:tcPr>
              <w:p w:rsidR="00C510CC" w:rsidRDefault="00C510CC">
                <w:r>
                  <w:t>On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document.</w:t>
                </w:r>
              </w:p>
              <w:p w:rsidR="00C510CC" w:rsidRDefault="00C510CC"/>
              <w:p w:rsidR="00C510CC" w:rsidRDefault="00C510CC"/>
            </w:tc>
    	
    



    Please remember to click the “Mark as answer” if this
    explanation resolves your dilemma.

    Regards,

    Chris Jensen

    Senior Technical Support Lead


    Chris Jensen

    • Proposed as answer by cjatms Friday, February 24, 2012 3:21 PM
    • Marked as answer by Forrest Guo Sunday, February 26, 2012 5:48 AM
    Wednesday, February 15, 2012 3:32 PM