【问题标题】:How to add text in a created table in a richtextbox?如何在富文本框中创建的表格中添加文本?
【发布时间】:2022-05-05 20:02:35
【问题描述】:

我在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


    【解决方案1】:

    如果你想动态插入文本,你应该声明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) 方法,肯定会有帮助

    【讨论】:

    • 你能告诉我如何使用 indexof 吗?
    【解决方案2】:

    在您的代码中添加 AppendLine 以添加文本。 我希望它会工作

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

    【讨论】:

      【解决方案3】:

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

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

      在上述示例中,每行有 3 个单元格。所以,你应该把你的文本放在 \intbl 和 \row 之间。例如:

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

      【讨论】:

        【解决方案4】:

        我们可以用硬编码数据填充单元格,如下 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表格中,这些示例见链接Add text in a created table in a richtextbox

        【讨论】:

          【解决方案5】:

          在行中加上数据

                          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();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-01-26
            • 2014-10-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-13
            • 1970-01-01
            相关资源
            最近更新 更多