Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue May 15, 2018 6:52 am

Hello,
I'm trying to write a code. there are a lot of checkboxes for example checkbox1 checkbox2 checkbox3 .etc , until checkbox is checked I have some text in word table rows if checkbox is not checked I want to remove the row where is the text written,
I managed to make it for separete checkboxes, but when I want 2 checkboxes or 3checkboxes simultaniously unchecked table row numbers are changing and overall I can't make it. I want to make a loop with checkboxes together but couldn't do it because of dynamic table if any ideas I will appreciate
thank you very much,
Code: Select all
   
  var vizireba = GetTable(document, "vizireba");
            var vizireba1 = GetTable(document, "vizireba1");
// 1
            if (checkBox39.Checked == false)
            {
                vizireba.Rows.RemoveAt(2);
                vizireba1.Rows.RemoveAt(1);
            }
           // 2
            if (checkBox46.Checked == false)
            {
                vizireba.Rows.RemoveAt(1);
                vizireba1.Rows.RemoveAt(0);
            }
            //3
            if (checkBox45.Checked == false)
            {
                vizireba.Rows.RemoveAt(3);
                vizireba1.Rows.RemoveAt(2);
            }

           // 4
                if (checkBox34.Checked == false)
            {
                vizireba.Rows.RemoveAt(4);
                vizireba1.Rows.RemoveAt(3);
            }

            //5
            if (checkBox43.Checked == false)
            {
                vizireba.Rows.RemoveAt(5);
                vizireba1.Rows.RemoveAt(4);
            }



I tried to combine 2 chekboxes (then 3 checkboxes) toghether with && the first one is working but second one only removes the first text from the table
Code: Select all
     if (checkBox46.Checked == false && checkBox39.Checked == false)
            {
                vizireba.Rows.RemoveAt(1);
                vizireba.Rows.RemoveAt(1);
                vizireba1.Rows.RemoveAt(0);
                vizireba1.Rows.RemoveAt(0);
         
            }
            else if (checkBox39.Checked == false && checkBox46.Checked != false)
            {
                vizireba.Rows.RemoveAt(2);
                vizireba1.Rows.RemoveAt(1);
            }
            else if (checkBox46.Checked == false && checkBox39.Checked != false)
            {
                vizireba.Rows.RemoveAt(1);
                vizireba1.Rows.RemoveAt(0);
            }
            //this is not working
         else if (checkBox46.Checked == false && checkBox45.Checked == false && checkBox39.Checked != false)
            {
                vizireba.Rows.RemoveAt(1);
                vizireba.Rows.RemoveAt(2);
                vizireba1.Rows.RemoveAt(0);
                vizireba1.Rows.RemoveAt(1);

kotechkh
 
Posts: 6
Joined: Thu Jun 01, 2017 1:56 pm

Tue May 15, 2018 8:44 am

Hello,

Thanks for your post.
To help us look into the case, could you please provide your sample word document?
We would keep it confidential.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue May 15, 2018 9:22 am

Hello,
Thank you very much
please see the attached file Whole document is written in Georgian The tables are located on the 4th and 6th page.
Sincerely,
Konstantine

kotechkh
 
Posts: 6
Joined: Thu Jun 01, 2017 1:56 pm

Tue May 15, 2018 10:11 am

Hello,

Thanks for your response.
I have tested your file with the latest version(Spire.Doc Pack(hot fix) Version:6.5.2), yer didn't encounter the issue.
Below is my testing code and attached the result. Please check twice.
Code: Select all
Document doc = new Document(path);

Table vizireba1 = null;
Table vizireba = null;

foreach (Table tb in doc.Sections[0].Tables)
{
    if (tb.Title == "vizireba1")
    {
        vizireba1 = tb;
    }
    if (tb.Title == "vizireba")
    {
        vizireba = tb;
    }
}

if (vizireba!=null)
{
    vizireba.Rows.RemoveAt(1);
    vizireba.Rows.RemoveAt(2);
}
if (vizireba1!=null)
{
    vizireba1.Rows.RemoveAt(0);
    vizireba1.Rows.RemoveAt(1);
}

doc.SaveToFile("13811.docx");


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue May 15, 2018 11:03 am

I've checked your code thank you for your work.
I think I couldn't explain it well. I have a problem I can't solve. I''ll try to make my idea simple
for example: there is a basket (in our condition basket is Table ) and the basket is full of fruits (when the program starts every checkbox is already checked every fruit is in the basket) 1) Apple 2)Mango 3) Banana 4)Orange if I want to take out apple from the basket I uncheck the checkbox so table.rows.removeat(1) (it will remove Apple from the table).
now Assume that We want to take out any random fruits together for example apple and mango so it will be table.rows.removeat(1) and removeat(3) but If apple is removed there is 1 less row in the table and for Mango The row number 3 is already changed and it will be 2 with hard coding I can calculate all the possible numbers for the row and write down removeat(2) instead of removeat(3) for the mango (I couldnt find any easier solution ) but still it doesn't work always so with my code.
1) how can I remove 2 or more any random fruits from the table with removing row
2) can I do it with loop not to hard code the possible numbers which the program must remove the row
Please see the attached file,
Thank you for your help, I appreciate it
Sincerely,
Konstantine

kotechkh
 
Posts: 6
Joined: Thu Jun 01, 2017 1:56 pm

Wed May 16, 2018 3:22 am

Hello,

Thanks for your details.
Regarding your case, there's a much easier way, that is removing the rows according to the content rather than the index. Just locate the row which the text lies in and then remove the row. Below is the code for your kind reference.

Code: Select all
Document doc = new Document(path);
Section s = doc.Sections[0];

Table table = s.Tables[0] as Table;

RemoveFruitRow(table, "Apple");
RemoveFruitRow(table, "Mango");

doc.SaveToFile("13811_new.docx");

 private static void RemoveFruitRow(Table table, string fruit)
 {
     for (int i = 0; i < table.Rows.Count; i++)
     {
         for (int j = 0; j < table.Rows[i].Cells.Count; j++)
         {

             //find the text and remove the row
             foreach (Paragraph par in table.Rows[i].Cells[j].Paragraphs)
             {
                 if (par != null && par.Text == fruit)
                 {
                     table.Rows.RemoveAt(i);
                 }
             }
         }
     }
 }


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu May 17, 2018 9:33 am

Hello kotechkh,

Greetings from e-iceblue!
How is your issue now?
Your feedback would be greatly appreciated.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc