How To Add Text in A Created Table in A Richtextbox

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Dotnet Stu

Home Articles Tutorials Forums Career Advice Jobs .NET FAQs News Member Login

How to add text in a created table in a Search this website …

richtextbox?

In my previous article How to create Table InRichTextBox using C#, I explained in detail
how can we insert a table in richtextbox. A er that, I was seeing questions from
developers that How to add text in a created table in a RichTextBox? This motivated me
to write this post in which I will try to help you with the solution to insert data in rich
text box table.
Recent Posts
If you haven’t yet seen my other posts on topic table in RichTextBox ,read them also
Serialize Object to XML in C# | Write
here Object Data to XML File in C#
Table in RichTextBox With Text Wrap In Columns
What is the use of
Display rtf file correctly In RichTextBox
Application.EnableVisualStyles()?

By now, you were able to create a table in rich text box control. If you only need the XPATH With Default Namespace In C# –
application user to enter data in the created table, it is possible anyway.What if, you XPathNavigator

need to create a table with data in RichTextBoxcontrol.Keep reading, examples given


String Value Enum with Space in between
below will help you to programmatically populate data in the table created in – Description Attribute
RichTextBoxin C#.
What Is

Add text in a created table in a RichTextBox control SetCompatibleTextRenderingDefault


(false)
Two examples will be explained. First one will populate data one by one at the time of
table cell creation. The second example will populate table and data in a loop from
parameters passed through the method call.

Example 1: Insert text in a created table in a RichTextBox control Recent Forum Topics

What is the Di erence Between


This sample is to demonstrate how we can populate cells with hard coded data.You
IEnumerable and IQueryable in C#
can visit Sample 2 below to understand how to fetch data from a data table and
populate the table in Rich TextBox control. How to remove Header of the XML file in
C#?
C#
1 private static String InsertTableInRichTextBox() What is DBMS?
2 {
3       StringBuilder sringTableRtf = new StringBuilder(); What is RDBMS?
4  
5       sringTableRtf.Append(@"{\rtf1 ");
What is asp net framework ?
6  
7       //Prepare the header Row
8       sringTableRtf.Append(@"\trowd");
9  
10       //A cell with width 1000.
11       sringTableRtf.Append(@"\cellx1000");
12  
13       sringTableRtf.Append(@"\intbl   ID");
14  
15       //Another cell with width 1000.Endpoint at 2000(which is 1000+1000).
16       sringTableRtf.Append(@"\cellx2000");
17  
18       sringTableRtf.Append(@"\cell    Name");
19  
20       //Another cell with width 1000.Ending at 3000 (which is 2000+1000)
21       sringTableRtf.Append(@"\cellx3000");
22  
23       sringTableRtf.AppendFormat(@"\cell    City");
24  
25       //Another cell with width 1000.End at 4000 (which is 3000+1000)
26       sringTableRtf.Append(@"\cellx4000");
27  
28       sringTableRtf.Append(@"\cell    Country");
29  
30       //Add the created row
31       sringTableRtf.Append(@"\intbl \cell \row");
32  
33       //Add 3 data Rows.Give proper padding space between data.Notice the gap after cell.
34       sringTableRtf.Append(@"\intbl   1" + @"\cell    Raj" + @"\cell    Bangalore" + @"\cell   
35       sringTableRtf.Append(@"\intbl   2" + @"\cell    Peter" + @"\cell    Mumbai" + @"\cell   In
36       sringTableRtf.Append(@"\intbl   3" + @"\cell    Chris" + @"\cell    Delhi"+ @"\cell   Indi
37  
38       sringTableRtf.Append(@"\pard");
39  
40       sringTableRtf.Append(@"}");
41  
42       //PopulateTable(sringTableRtf);
43       return sringTableRtf.ToString();
44 }

The click of the button will create the table with data in cells. In button click event
Invoke the InsertTableInRichTextBox function and assign the return string to the
RichTextBox control. Update button click event as below.

C#
1 privatevoidbtnCreateTable_Click(object sender, EventArgs e)
2 {
3     //Insert Table in RichTextBox Control by setting .Rtf as the string returned.
4     //Set the RichTextBox width to fit the table completely.
5     this.richTextBox.Rtf = InsertTableInRichTextBox();
6 }

Output is,

But, Most of the cases, we need to populate RichTextBox table with data given in some
format rather than hard coding cell data.Next example explains how we can fetch data
from a data table and display it in a table created in RichTextBox control.

Example 2: Add text in a created table in a RichTextBox control | Insert DataTable


data to RichTextBox table

Read further on to understand how to insert data from a DataTable into a created table
in RichTextBox control. Overload the above sample method as below and update the
button click event accordingly.

C#
1 // Method to create a table format string with data from a DataTable.
2  
3 private static String InsertTableInRichTextBox(DataTabled tbl,int width)
4 {
5      //Since too much string appending go for string builder
6      StringBuilder sringTableRtf = newStringBuilder();
7  
8      //beginning of rich text format,dont customize this begining line
9      sringTableRtf.Append(@"{\rtf1 ");
10  
11      //create 5 rows with 3 cells each
12      intcellWidth;
13  
14      //Start the Row
15      sringTableRtf.Append(@"\trowd");
16  
17      //Populate the Table header from DataTable column headings.
18      for (int j = 0; j <dtbl.Columns.Count; j++)
19      {
20          //A cell with width 1000.
21          sringTableRtf.Append(@"\cellx" + ((j+1) * width).ToString());
22  
23          if (j == 0)
24          sringTableRtf.Append(@"\intbl  " + dtbl.Columns[j].ColumnName);
25          else
26          sringTableRtf.Append(@"\cell   " + dtbl.Columns[j].ColumnName);
27       }
28  
29       //Add the table header row
30       sringTableRtf.Append(@"\intbl \cell \row");
31  
32       //Loop to populate the table cell data from DataTable
33       for (inti = 0; i<dtbl.Rows.Count; i++)
34       {
35           //Start the Row
36           sringTableRtf.Append(@"\trowd");
37  
38           for (int j = 0; j <dtbl.Columns.Count; j++)
39           {
40              cellWidth = (j+1) * width;
41  
42              //A cell with width 1000.
43              sringTableRtf.Append(@"\cellx" + cellWidth.ToString());
44  
45              if (j == 0)
46              sringTableRtf.Append(@"\intbl  " + dtbl.Rows[i][j].ToString());
47              else
48              sringTableRtf.Append(@"\cell   " + dtbl.Rows[i][j].ToString());
49            }
50  
51            //Insert data row
52            sringTableRtf.Append(@"\intbl \cell \row");
53        }
54  
55        sringTableRtf.Append(@"\pard");
56        sringTableRtf.Append(@"}");
57  
58        //convert the string builder to string
59        return sringTableRtf.ToString();
60 }

In button click event, a dummy DataTable created and passed as a parameter along
with cell width.

C#
1 private void btnCreateTable_Click(object sender, EventArgs e)
2 {
3     //Create a DataTable with four columns.
4     DataTable dtbl = new DataTable();
5     dtbl.Columns.Add("ID", typeof(int));
6     dtbl.Columns.Add("Name", typeof(string));
7     dtbl.Columns.Add("City", typeof(string));
8     dtbl.Columns.Add("Country", typeof(string));
9  
10     //Here we add five DataRows.
11     dtbl.Rows.Add(1, "Ram", "Bangalore", "India");
12     dtbl.Rows.Add(2, "Manoj", "Mumbai", "India");
13     dtbl.Rows.Add(3, "Peter", "Chennai", "India");
14     dtbl.Rows.Add(4, "Eric", "Delhi", "India");
15  
16     //Insert Table in RichTextBox Control by setting .Rtf as the string returned.
17     //Set the RichTextBox width to fit the table completely,
18     this.richTextBox.Rtf = InsertTableInRichTextBox(dtbl,2000);
19 }

On execution, this will display result as,

Summary:
The above examples are simple solutions from my side. There can be other alternate
best solutions. Share your thoughts and comments. If you have better solutions to add
text in a created table in richtextbox, post it in the comment section below.

Happy Coding!

Related Posts:
Table in RichTextBox With Text Wrap In Columns(Cells)
Display rtf file correctly In RichTextBox – C#
How To Create Table in RichTextbox Using C#

Leave a Reply
Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Post Comment

© Copywright 2017 Dotnetstu s All Rights Reserved

You might also like