2012-09-24 4 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로 렌더링하고 그 tableRtf에 misc_tb.Rtf를 설정하십시오. 그런데 IndexOf 메서드와 같이 셀 위치를 정의하고 그 위치에 텍스트를 삽입해야합니다.

업데이트 : 어쩌면 theese 링크가 당신에게 도움이 될 것입니다 RtfTable 클래스의

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

도움이 될 것입니다 세포 (INT 행, INT의 COL) 확인 방법을 가지고 있습니다
+0

당신은 그와 같이 IndexOf PL하는 방법을 말해 줄 수 진정해? –

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 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 테이블에 DataTable에 데이터를 삽입 할 수 있습니다 : 예를 들면 : 우리는 3 행 아래로 하드 코딩 된 데이터와 셀을 채울 수 있습니다

tableRtf.Append(@"\intbl MyText1 \cell MyText2 \cell MyText3 \cell \row"); 
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(); 
관련 문제