In my previous article How to create Table InRichTextBox using C#, I explained in detail how can we insert a table in richtextbox. After 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.
If you haven’t yet seen my other posts on topic table in RichTextBox ,read them also here
Table in RichTextBox With Text Wrap In Columns
Display rtf file correctly In RichTextBox
By now, you were able to create a table in rich text box control. If you only need the application user to enter data in the created table, it is possible anyway.What if, you need to create a table with data in RichTextBoxcontrol.Keep reading, examples given below will help you to programmatically populate data in the table created in RichTextBoxin C#.
Add text in a created table in a RichTextBox control
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
This sample is to demonstrate how we can populate cells with hard coded data.You can visit Sample 2 below to understand how to fetch data from a data table and populate the table in Rich TextBox control.
private static String InsertTableInRichTextBox()
{
StringBuilder sringTableRtf = new StringBuilder();
sringTableRtf.Append(@"{\rtf1 ");
//Prepare the header Row
sringTableRtf.Append(@"\trowd");
//A cell with width 1000.
sringTableRtf.Append(@"\cellx1000");
sringTableRtf.Append(@"\intbl ID");
//Another cell with width 1000.Endpoint at 2000(which is 1000+1000).
sringTableRtf.Append(@"\cellx2000");
sringTableRtf.Append(@"\cell Name");
//Another cell with width 1000.Ending at 3000 (which is 2000+1000)
sringTableRtf.Append(@"\cellx3000");
sringTableRtf.AppendFormat(@"\cell City");
//Another cell with width 1000.End at 4000 (which is 3000+1000)
sringTableRtf.Append(@"\cellx4000");
sringTableRtf.Append(@"\cell Country");
//Add the created row
sringTableRtf.Append(@"\intbl \cell \row");
//Add 3 data Rows.Give proper padding space between data.Notice the gap after cell.
sringTableRtf.Append(@"\intbl 1" + @"\cell Raj" + @"\cell Bangalore" + @"\cell India" + @"\row");
sringTableRtf.Append(@"\intbl 2" + @"\cell Peter" + @"\cell Mumbai" + @"\cell India" + @"\row");
sringTableRtf.Append(@"\intbl 3" + @"\cell Chris" + @"\cell Delhi"+ @"\cell India" + @"\row");
sringTableRtf.Append(@"\pard");
sringTableRtf.Append(@"}");
//PopulateTable(sringTableRtf);
return sringTableRtf.ToString();
}
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.
privatevoidbtnCreateTable_Click(object sender, EventArgs e)
{
//Insert Table in RichTextBox Control by setting .Rtf as the string returned.
//Set the RichTextBox width to fit the table completely.
this.richTextBox.Rtf = InsertTableInRichTextBox();
}
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.
// Method to create a table format string with data from a DataTable.
private static String InsertTableInRichTextBox(DataTabled tbl,int width)
{
//Since too much string appending go for string builder
StringBuilder sringTableRtf = newStringBuilder();
//beginning of rich text format,dont customize this begining line
sringTableRtf.Append(@"{\rtf1 ");
//create 5 rows with 3 cells each
intcellWidth;
//Start the Row
sringTableRtf.Append(@"\trowd");
//Populate the Table header from DataTable column headings.
for (int j = 0; j <dtbl.Columns.Count; j++)
{
//A cell with width 1000.
sringTableRtf.Append(@"\cellx" + ((j+1) * width).ToString());
if (j == 0)
sringTableRtf.Append(@"\intbl " + dtbl.Columns[j].ColumnName);
else
sringTableRtf.Append(@"\cell " + dtbl.Columns[j].ColumnName);
}
//Add the table header row
sringTableRtf.Append(@"\intbl \cell \row");
//Loop to populate the table cell data from DataTable
for (inti = 0; i<dtbl.Rows.Count; i++)
{
//Start the Row
sringTableRtf.Append(@"\trowd");
for (int j = 0; j <dtbl.Columns.Count; j++)
{
cellWidth = (j+1) * width;
//A cell with width 1000.
sringTableRtf.Append(@"\cellx" + cellWidth.ToString());
if (j == 0)
sringTableRtf.Append(@"\intbl " + dtbl.Rows[i][j].ToString());
else
sringTableRtf.Append(@"\cell " + dtbl.Rows[i][j].ToString());
}
//Insert data row
sringTableRtf.Append(@"\intbl \cell \row");
}
sringTableRtf.Append(@"\pard");
sringTableRtf.Append(@"}");
//convert the string builder to string
return sringTableRtf.ToString();
}
In button click event, a dummy DataTable created and passed as a parameter along with cell width.
private void btnCreateTable_Click(object sender, EventArgs e)
{
//Create a DataTable with four columns.
DataTable dtbl = new DataTable();
dtbl.Columns.Add("ID", typeof(int));
dtbl.Columns.Add("Name", typeof(string));
dtbl.Columns.Add("City", typeof(string));
dtbl.Columns.Add("Country", typeof(string));
//Here we add five DataRows.
dtbl.Rows.Add(1, "Ram", "Bangalore", "India");
dtbl.Rows.Add(2, "Manoj", "Mumbai", "India");
dtbl.Rows.Add(3, "Peter", "Chennai", "India");
dtbl.Rows.Add(4, "Eric", "Delhi", "India");
//Insert Table in RichTextBox Control by setting .Rtf as the string returned.
//Set the RichTextBox width to fit the table completely,
this.richTextBox.Rtf = InsertTableInRichTextBox(dtbl,2000);
}
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!
Leave a Reply