如何在richtextbox中创建的表格中添加文本?

问题描述 投票:0回答:5

我在

richtextbox
中创建了一个表,如下所示:

       //Since too much string appending go for string builder
       StringBuilder tableRtf = new StringBuilder();

       //beginning of rich text format,dont customize this begining line              
    tableRtf.Append(@"{\rtf1 ");             

    //create 5 rows with 3 cells each

        for (int i = 0; i < 5; i++)
        {

            tableRtf.Append(@"\trowd");

           //A cell with width 1000.
            tableRtf.Append(@"\cellx1000"); 

            //Another cell with width 2000.end point is 3000 (which is 1000+2000).
            tableRtf.Append(@"\cellx2000"); 

            //Another cell with width 1000.end point is 4000 (which is 3000+1000)
            tableRtf.Append(@"\cellx3000");

            //Another cell with width 1000.end point is 4000 (which is 3000+1000)
            tableRtf.Append(@"\cellx4000");


            tableRtf.Append(@"\intbl \cell \row"); //create row

        }

        tableRtf.Append(@"\pard");

        tableRtf.Append(@"}");

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

现在我想知道如何将文本放入标题和每个单元格中。

你有什么想法吗?

c# richtextbox rtf
5个回答
0
投票

如果要动态插入文本,则应声明DataTable,例如: DataTable dt,您可以使用 dt 动态更改数据。每次更改后,您应该渲染到 tableRtf 并将该 tableRtf 设置为misc_tb.Rtf。 换句话说,您应该定义单元格位置(使用类似 IndexOf 方法)并在该位置插入文本。

更新: 也许这些链接会对您有所帮助:

http://space.dl.sourceforge.net/project/netrtfwriter/netrtfwriter/latest%20-%200.9.1/RtfWriter_V0.9.1.zip

http://www.codeproject.com/Articles/11306/NRTFTree-A-class-library-for-RTF-processing-in-C

在 RtfTable 类中有 cell(int row, int col) 方法,肯定会有帮助


0
投票

在代码中添加 AppendLine 以添加文本。 我希望它会起作用

tableRtf.Append(@"\cellx1000").AppendLine(" ID");       
tableRtf.Append(@"\cellx2000").AppendLine(" First Name");         
tableRtf.Append(@"\cellx3000").AppendLine(" Lastname");       
tableRtf.Append(@"\cellx4000").AppendLine(" Salary");

0
投票

您可以将文本放在循环的最后一行,即:

tableRtf.Append(@"\intbl \cell \row");

在上述示例中,每行有 3 个单元格。因此,您应该将文本放在 \intbl 和 噢。例如:

tableRtf.Append(@"\intbl  MyText1 \cell  MyText2 \cell  MyText3 \cell \row");

0
投票

我们可以用硬编码数据填充单元格,如下 3 行填充数据:

      tableRtf.Append(@"\intbl   1" + @"\cell    Raj" + @"\cell    Bangalore" + @"\cell    India" + @"\row");
      tableRtf.Append(@"\intbl   2" + @"\cell    Peter" + @"\cell    Mumbai" + @"\cell   India" + @"\row");
      tableRtf.Append(@"\intbl   3" + @"\cell    Chris" + @"\cell    Delhi"+ @"\cell   India" + @"\row");

还可以循环将 DataTable 数据插入 RichTextBox 表,有关这些示例,请参阅链接 在 RichTextBox 中创建的表中添加文本


0
投票

加上行中的数据

                StringBuilder tableRtf = new StringBuilder();

                tableRtf.Append(@"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}");

                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {   

                    tableRtf.Append(@"\trowd");
                    tableRtf.Append(@"\cellx2500" + "  " + ds.Tables[0].Rows[j]["caption"].ToString());
                    tableRtf.Append(@"\intbl\cell");
                    tableRtf.Append(@"\cellx10000\intbl\cel");
                    tableRtf.Append("   "+ ds2.Tables[0].Rows[k][j].ToString() + @"\intbl\clmrg\cell\row");

                }

                tableRtf.Append(@"\pard");
                tableRtf.Append(@"}");                    
                richTextBox2.Rtf = tableRtf.ToString();
© www.soinside.com 2019 - 2024. All rights reserved.