简体   繁体   English

使用Rtf在Richtextbox中创建表

[英]Creating a table in a Richtextbox using Rtf

I've been on this for the whole day now. 我整天都在忙这个。 I would like to have a table in a Richtextbox with these columns: 我想在Richtextbox中有一个包含这些列的表:

ITEMS                  |   QTY  |  Amount  |    
Sample 1               |    1   |  100.00  |
Another Sample Here    |    3   |  300.00  |
And A Sample Here      |    4   |  400.00  |

I have gotten to this point 我已经到了这一点

var tableRtf = new StringBuilder();

        tableRtf.Append(@"{\rtf1 ");
        tableRtf.Append(@"{\rtf1\pc \qc \b SOME NAME \b0 \par}");
        tableRtf.Append(@"{\rtf1\pc \qc SOME TYPE \par}");
        tableRtf.Append(@"{\rtf1\pc \qc SOMEWHERE IN THE CITY, SOMETHING ADDRESS, SOMETHING CITY \par}");
        tableRtf.Append(@"{\rtf1\pc \qc TEL. NUM. 1234567 \par}");
        tableRtf.Append(@"{\rtf1\pc \qc VAT-REG TIN 200-035-311-0010 \par}");
        tableRtf.Append(@"{\rtf1\pc \par}");

        for (var i = 0; i < listOrderedItems.Items.Count; i++)
            {
                var x = listOrderedItems.Items[i].Text;
                var y = listOrderedItems.Items[i].SubItems[3].Text;
                var q = listOrderedItems.Items[i].SubItems[2].Text;

                tableRtf.Append(@"\trowd");

                //A cell with width 4000.
                tableRtf.Append(@"\cellx4000");
                //Another cell with width 1000
                tableRtf.Append(@"\cellx5000");
                //Another cell with width 1000
                tableRtf.Append(@"\cellx6000");

                tableRtf.Append(String.Format(@"\intbl {0} \cell {1} \cell {2} \cell \row",x,q,y)); //create row

            }

            tableRtf.Append(@"\pard");

            tableRtf.Append(@"}");

            this.txtReceipt.Rtf = tableRtf.ToString(); 

How do I put the column headers and how do I center the text in the second and third columns only? 如何放置列标题,如何仅在第二列和第三列中居中放置文本? Your help is greatly appreciated. 非常感谢您的帮助。

It is possible to directly hard code the column header or insert column header and data from a DataTable into a created table in RichTextBox control. 可以直接对列标题进行硬编码,也可以将列标题和数据从DataTable插入RichTextBox控件中创建的表中。

For example : dtbl is a datatable which we can trasfer in to richtextbox table. 例如: dtbl是一个数据表,我们可以将其转移到richtextbox表中。 tableRtf is a text string. tableRtf是文本字符串。

 for (int i = 0; i <dtbl.Columns.Count; j++)
 {
     //A cell with width 1000. 
     tableRtf.Append(@"\cellx" + ((i+1) * 1000).ToString());

     if (i == 0)
     tableRtf.Append(@"\intbl  " + dtbl.Columns[i].ColumnName);
     else
     tableRtf.Append(@"\cell   " + dtbl.Columns[i].ColumnName);
  }

For more details Check out the link add text to created table Richtextbox 有关更多详细信息,请查看链接, 向创建的表Richtextbox添加文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM