'How to add text in a created table in a richtextbox?

I created a table in richtextbox like this :

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

Now I want to know how I can put text in headers and in each cells.

Do you have an idea?



Solution 1:[1]

If you want to insert text dynamically, you should declare DataTable, for example: DataTable dt and you can change data dynamically with dt. After changing each time you should render to tableRtf and set to misc_tb.Rtf that tableRtf. In othe way, you should define cell location (with like IndexOf method) and insert text that location.

Updated: maybe theese links are will help you:

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

in RtfTable class has cell(int row, int col) method, sure it will help

Solution 2:[2]

Add AppendLine in Your Code to add text. I hope it will work

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

Solution 3:[3]

You can put your text in last line in the loop, that is:

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

In mentioned example, you have 3 cell in each row. So, you should put your text between \intbl and \row. For example:

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

Solution 4:[4]

We can populate cells with hard coded data as below 3 rows are populated with data:

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

Also can Insert DataTable data to RichTextBox table in loop ,for these samples see the link Add text in a created table in a richtextbox

Solution 5:[5]

plus data in row

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Go Goal
Solution 3 Shadow Wizard Says No More War
Solution 4 RBT
Solution 5 Cza