2012-09-24 28 views
0

richtextbox这样创建的表:如何在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(); 

现在我想知道我怎么可以把文字,标题和每个细胞。

你有什么想法吗?

回答

0

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

更新时间: 也许theese环节都将帮助您:

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类有细胞(INT行,诠释山口)方法,相信这将有助于

+0

你能告诉我怎么做,与PL的indexOf缓解? –

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和\ row之间。例如:

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

还可以在环中插入数据表的数据的RichTextBox表,对于这些样品看到行链路Add text in a created table in a 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(); 
相关问题