We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

What is the best way to delete a number of colums from a table?

Thread ID:

Created:

Updated:

Platform:

Replies:

145076 Jun 5,2019 12:57 PM UTC Jun 7,2019 09:28 AM UTC ASP.NET Core - EJ 2 3
loading
Tags: DocIO
Pieter van Kampen
Asked On June 5, 2019 12:57 PM UTC

Hi,
I am using a template document, with a formatted table in a bookmark. I can find the table and then change it.
However, I would like to start with a fixed set of rows and columns. 
When using 
     table.ResetCells(1, 2);

It also clears the content of the cells. There does not seem to be an option to retain the content when doing ResetCells. So what is the best alternative?

I now do this, please check if this is the most efficient. Perhaps I should make an extension method?
First step is reduce to 1 row and just a few cells to ensure the unwanted columns and cell content is removed:

    if (table== null) return;
    //reduce to just the header
    for (var index = table.Rows.Count - 1; index > 0; index--)
    {
        table.Rows.Remove(table.Rows[index]);
    }
    for (var index = table.Rows[0].Cells.Count - 1; index > 2; index--)
    {
        table.Rows[0].Cells.RemoveAt(index);
    }

Next expand the number of columns dynamically to the desired, and add the first content row to be filled:

    //add the needed cells for the dates
    for (int j = 0; j <= dateCount; j++)
    {
        table.Rows[0].Cells.Add(new WTableCell(_wordDocument));
        table[0, 3 + j].Width = dateWidth;
    }
     // add the first row
    table.AddRow(true, true);
           

Thanks, Pieter             

Vijayasurya Anandhan [Syncfusion]
Replied On June 6, 2019 09:59 AM UTC

Hi Pieter,

Thank you for contacting Syncfusion support.

From the given details, we have found that your end requirement is to find the table and modify the contents. In DocIO, ResetCells API is used to reset the table with the specified number of rows and columns and it doesn’t preserve the existing contents of the table, which is one of the behaviors of ResetCells API. To meet your requirement, it needs to iterate through the table elements and modify the contents as per your code example.

Please let us know if you have any other questions.

Regards,
Vijayasurya A
 


Pieter van Kampen
Replied On June 6, 2019 09:30 PM UTC

Thank you,
I made the following extension method, which can reduce a table size or extend.

        public static void ResetCellsWithContent(this WTable table, int rows, int cols)
        {
            if (rows == 0 || cols == 0) throw new ArgumentException("rows and cols should be more than 0");

            if (rows < table.Rows.Count) //reduce the rows
            {
                for (var index = table.Rows.Count - 1; index > rows - 1; index--)
                {
                    table.Rows.Remove(table.Rows[index]);
                }
            }

            if (cols < table.Rows[0].Cells.Count) //reduce cols
            {
                for (var rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
                {
                    for (var index = table.Rows[rowIndex].Cells.Count - 1; index > cols - 1; index--)
                    {
                        table.Rows[rowIndex].Cells.RemoveAt(index);
                    }
                }
            }

            if (cols > table.Rows[0].Cells.Count)
            { //add cells to each existing row

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    for (int index = table.Rows[i].Cells.Count; index < cols ; index++)
                    {
                        table.Rows[i].Cells.Add(new WTableCell(table.Document));

                    }
                }
            }

            if (rows <= table.Rows.Count) return;

            for (int i = table.Rows.Count; i < rows; i++)
            {
                table.AddRow(true, true);
            }

        }

Mohanaselvam  Jothi [Syncfusion]
Replied On June 7, 2019 09:28 AM UTC

Hi Pieter,

Thank you for your update.

We are happy to hear that the solution for your requirement is found.  If you face any problems to achieve your requirements, kindly share us the input Word document and expected output document. Thereby, we will check and provide you the solution at the earliest.

Please let us know if you have any other question

Regards,
Mohanaselvam J



CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon