locked
i have code to add a row to a table in richtextbox how do i delete a column? RRS feed

  • Question

  • I have a table in richtextbox code I wanted to delete a column although the overall size of the table to remain the same.  So to resize the column and delete a column to have the table the same size

    Sincerely

    Mike

    examplecode

    https://drive.google.com/open?id=1948-d-Hv6msQxUMaPmv4Bx_q0Pto3AUe

    Friday, November 9, 2018 7:48 PM

Answers

  • Hi   Michael J Sobol, 

    >>I have a table in richtextbox code I wanted to delete a column although the overall size of the table to remain the same.  So to resize the column and delete a column to have the table the same size

    You can try the following steps.

    1: remove column and cells

    2: calculate the  new  each column width and resize column width

    3: refresh the row/column index

    You can refer the following sample and try to Implement your own details.

    private void TableCell_MouseDown(object sender, MouseButtonEventArgs e)
            {
                //---------------add new column
                if (sender != null)
                {
                    TableCell _cell = sender as TableCell;
                    showtxt.Content = _cell.Tag;
                    //get the current column index and insert a column   under the current column
                    int currentcolumn = Convert.ToInt32(_cell.Tag.ToString().Split(';')[1].Substring(0, 1));
    
                    //add colum
                    if (t.RowGroups[0] != null && checknewrow.IsChecked == true)
                    {
                        int currentrowindex = t.RowGroups[0].Rows.Count;
                        double newpagemargin2 = 0.1;
                        double eachcellwidth = ((550 - 2 * (newpagemargin2)) / Convert.ToInt32(ColumnNum.SelectedValue.ToString()));
    
                        double precentpagewidth = Convert.ToInt32(percentColumn.SelectedValue.ToString()) * 0.95 / 100.0;
    
                        t.Columns.Insert(currentcolumn + 1, new TableColumn() { Width = new GridLength(eachcellwidth * precentpagewidth, GridUnitType.Pixel) });
    
    
                        for (int i = 0; i < currentrowindex; i++)
                        {
                            System.Windows.Documents.TableRow tr = t.RowGroups[0].Rows[i];
                            System.Windows.Documents.TableCell tableCell = new System.Windows.Documents.TableCell() { BorderBrush = System.Windows.Media.Brushes.Black, BorderThickness = new Thickness(CellBorderThickness.IsChecked == true ? 1 : 0) };
                            var p = new Paragraph();
                            p.Inlines.Add(new TextBlock()
                            {
                                Text = " ",
                                // TextWrapping = TextWrapping.NoWrap
                            });
                            tableCell.MouseDown += TableCell_MouseDown;
                            tableCell.Blocks.Add(p);
                            tr.Cells.Insert(currentcolumn + 1, tableCell);
                            //tr.Cells.RemoveAt(currentcolumn);
                        }
    
                        // refresh the row/column index
                        for (int i = 0; i < t.RowGroups[0].Rows.Count; i++)
                        {
                            System.Windows.Documents.TableRow tr = t.RowGroups[0].Rows[i];
                            for (int j = 0; j < tr.Cells.Count; j++)
                            {
                                System.Windows.Documents.TableCell tc = tr.Cells[j];
                                tc.Tag = i + ": row;" + j + ": Column";
                            }
                        }
                        showtxt.Content = _cell.Tag + "; Table Row Count: " + t.RowGroups[0].Rows.Count.ToString();
                    }
                    //delete column
                    else if(t.RowGroups[0] != null && deletecolumn.IsChecked == true)
                    {
                        int currentrowindex = t.RowGroups[0].Rows.Count;
    
                        double totalwidth = (t.Columns[0].Width.Value) * (t.Columns.Count);
    
                        //----------steps 1-----------------
                        // remove column
                        t.Columns.RemoveAt(currentcolumn);
                        // remove cells
                        for (int i = 0; i < currentrowindex; i++)
                        {
                            System.Windows.Documents.TableRow tr = t.RowGroups[0].Rows[i];
                            tr.Cells.RemoveAt(currentcolumn);
                        }
    
                        //----------steps 2-----------------
                        // new  each column width
                        double eachcolumnwidth = (totalwidth / (t.Columns.Count));
                        // resize column with
                        for (int i = 0; i < t.Columns.Count; i++)
                        {
                            System.Windows.Documents.TableColumn tc = t.Columns[i];
                            tc.Width = new GridLength(eachcolumnwidth, GridUnitType.Pixel);
                        }
    
                        //----------steps 3-----------------
                        // refresh the row/column index
                        for (int i = 0; i < t.RowGroups[0].Rows.Count; i++)
                        {
                            System.Windows.Documents.TableRow tr = t.RowGroups[0].Rows[i];
                            for (int j = 0; j < tr.Cells.Count; j++)
                            {
                                System.Windows.Documents.TableCell tc = tr.Cells[j];
                                tc.Tag = i + ": row;" + j + ": Column";
                            }
                        }
                        showtxt.Content = _cell.Tag + "; Table Row Count: " + t.RowGroups[0].Rows.Count.ToString();
                    }
                }
            }
    
    

    Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. This will help other members to find the solution quickly if they have faced the similar issue. If you have a new question you can start a new thread.

    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 12, 2018 3:46 AM