Professional Documents
Culture Documents
How To Add Text in A Created Table in A Richtextbox
How To Add Text in A Created Table in A Richtextbox
How To Add Text in A Created Table in A Richtextbox
Home Articles Tutorials Forums Career Advice Jobs .NET FAQs News Member Login
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
Example 1: Insert text in a created table in a RichTextBox control Recent Forum Topics
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.
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 }
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