create borders in code

2 posts, 0 answers
  1. RAMÓN
    RAMÓN avatar
    1 posts
    Member since:
    Nov 2015

    Posted 15 Jun Link to this post

    As I can build a table in code that handles at will the borders of the cells. Not manage to control how I want the borders.

     

    TableBorders bordetabla = new TableBorders();
    bordetabla.SetBottom(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
    bordetabla.SetLeft(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    bordetabla.SetRight(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    bordetabla.SetTop(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    bordetabla.SetInsideHorizontal(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    bordetabla.SetInsideVertical(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
     
     
    TableCellBorders bordeBajo = new TableCellBorders();
    bordeBajo.SetBottom(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
    bordeBajo.SetLeft(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    bordeBajo.SetRight(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    bordeBajo.SetTop(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    bordeBajo.SetInsideHorizontal(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
    bordeBajo.SetInsideVertical(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
     
     
    TableCellBorders Sinborde = new TableCellBorders();
    Sinborde.SetBottom(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    Sinborde.SetLeft(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    Sinborde.SetRight(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    Sinborde.SetTop(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    Sinborde.SetInsideHorizontal(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
    Sinborde.SetInsideVertical(new Border(Telerik.WinForms.Documents.Model.BorderStyle.None));
     
    TableCellBorders todoborde = new TableCellBorders();
    todoborde.SetBottom(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
    todoborde.SetLeft(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
    todoborde.SetRight(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
    todoborde.SetTop(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
    todoborde.SetInsideHorizontal(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
    todoborde.SetInsideVertical(new Border(Telerik.WinForms.Documents.Model.BorderStyle.Single, Telerik.WinControls.RichTextEditor.UI.Colors.Black));
     
     
     
     
     
    Table tabla;
    Paragraph parrafo;
    Span bloque;
    TableRow fila;
    TableCell Celda;
     
    tabla = new Table();
    tabla.LayoutMode = TableLayoutMode.AutoFit;
    tabla.StyleName = RadDocumentDefaultStyles.DefaultNormalTableStyleName;               
    //tabla.Borders = bordetabla;
     
     
     
    fila = new TableRow();
    Celda = new TableCell();
    Celda.ColumnSpan = 2;
    Celda.Borders = bordeBajo;
    parrafo = new Paragraph();
    bloque = new Span();
    bloque.Text = "PRIMEROS PLATOS";
    parrafo.Inlines.Add(bloque);
    Celda.Blocks.Add(parrafo);
    fila.Cells.Add(Celda);
    tabla.Rows.Add(fila);
     
     
    fila = new TableRow();
     
    Celda = new TableCell();
    //Celda.Borders = Sinborde;
    parrafo = new Paragraph();
    bloque = new Span();
    bloque.Text = "2";
    parrafo.Inlines.Add(bloque);
    Celda.Blocks.Add(parrafo);
    fila.Cells.Add(Celda);
     
     
    Celda = new TableCell();
    //Celda.Borders = Sinborde;
    parrafo = new Paragraph();
    bloque = new Span();
    bloque.Text = "Naranjas de la china";
    parrafo.Inlines.Add(bloque);
    Celda.Blocks.Add(parrafo);
    fila.Cells.Add(Celda);
     
    tabla.Rows.Add(fila);

  2. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 16 Jun Link to this post

    Hello Ramon,

    Thank you for writing.

    In order to achieve this task, you would need to create a TableCellBorders border and pass in the constructor the four borders. Please check my code snippet below: 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            RadDocument document = new RadDocument();
            Section section = new Section();
     
            Table table = new Table();
            table.StyleName = RadDocumentDefaultStyles.DefaultTableGridStyleName;
     
            for (int i = 0; i < 6; i++)
            {
                TableRow row1 = new TableRow();
                TableCell cell1 = new TableCell();
                Paragraph p1 = new Paragraph();
                Span s1 = new Span();
                s1.Text = "Cell " + i;
                p1.Inlines.Add(s1);
     
                Border left = new Border(1, Telerik.WinForms.Documents.Model.BorderStyle.None, Colors.Black);
                Border top = new Border(1, Telerik.WinForms.Documents.Model.BorderStyle.None, Colors.Black);
                Border right = new Border(1, Telerik.WinForms.Documents.Model.BorderStyle.None, Colors.Black);
                Border bottom = new Border(1, Telerik.WinForms.Documents.Model.BorderStyle.None, Colors.Black);
     
                if (i == 0 || i == 4)
                {
                    bottom = new Border(1, Telerik.WinForms.Documents.Model.BorderStyle.Single, Colors.Black);
                }
     
                cell1.Borders = new TableCellBorders(left, top, right, bottom);
     
                cell1.Blocks.Add(p1);
                row1.Cells.Add(cell1);
     
                TableCell cell2 = new TableCell();
                Paragraph p2 = new Paragraph();
                Span s2 = new Span();
                s2.Text = "Cell " + i;
                p2.Inlines.Add(s2);
                cell2.Blocks.Add(p2);
                row1.Cells.Add(cell2);
     
                cell2.Borders = new TableCellBorders(left, top, right, bottom);
                table.Rows.Add(row1);
            }
     
            section.Blocks.Add(new Paragraph());
            section.Blocks.Add(table);
            section.Blocks.Add(new Paragraph());
            document.Sections.Add(section);
     
            this.radRichTextEditor1.Document = document;
        }
    }

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top