Reputation: 25
In my project, I'm using a StringBuilder
to create a table in RichEditBox
. But in my column the data value is differ from each other. So it looks like shuffled data. But my excepted output is to be perfectly fix the length for the data in table. I can see the table as properly completed in my output .
Can anyone say how to fix the length to the string in StringBuilder
in C#?
My code:
Connection();
try
{
string Today = "Student Test Mark Details";
string Line = "----------------------------------";
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd = new SqlCommand("select ExaminationName, ExaminationCenter, ExaminationDate, Subjects, MarkObtained, Total, Percentage, Grade from StudentMarksHistory where StudentCode='" + ICBEStudentCode.Text + "' and ExaminationName='" + TBExaminationName.Text + "' and ClassName='" + ICBEClassSection.Text + "' and Remark='Record Saved'", cs);
StringBuilder paragraph = new StringBuilder();
SqlDataReader dr = cmd.ExecuteReader();
paragraph.Append(Today).Append("\t\n");
paragraph.Append(Line).Append("\t\n\n\n\n\n");
paragraph.Append("ExamName").Append("\t");
paragraph.Append("ExamCenter").Append("\t");
paragraph.Append("ExamDate").Append("\t");
paragraph.Append("Subject").Append("\t\t\t");
paragraph.Append("Mark").Append("\t");
paragraph.Append("Total").Append("\t");
paragraph.Append("Percentage").Append("\t");
paragraph.Append("Grade").Append("\n\n");
while (dr.Read())
{
DateTime DateName = Convert.ToDateTime(dr["ExaminationDate"]);
string subject = dr["Subjects"].ToString().Trim();
//int Len = subject.Length;
//subject = subject.ToString().PadRight(50 - Len, ' ');
paragraph.Append(dr["ExaminationName"].ToString()).Append("\t");
paragraph.Append(dr["ExaminationCenter"].ToString()).Append("\t");
paragraph.Append(DateName.ToString("dd/MM/yyyy")).Append("\t");
paragraph.Append(subject);
paragraph.Append(' ', 15 - subject.Length);
//paragraph.Append(subject.PadRight(100));
paragraph.Append(dr["MarkObtained"].ToString()).Append("\t");
paragraph.Append(dr["Total"].ToString()).Append("\t");
paragraph.Append(dr["Percentage"].ToString()).Append("\t");
paragraph.Append(dr["Grade"].ToString()).Append("\n");
}
string Notes = "************[ Minimum Pass Mark is 35 ]*************";
paragraph.Append(" ").Append("\n\n\n\n");
paragraph.Append(Notes).Append("\r\n");
dr.Close();
cs.Close();
}
RTBMessage.Text = paragraph.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Results expceted:
Student Test Mark Details
----------------------------------
ExamName ExamCenter ExamDate Subject Mark Total
Class Test 1 Room No1 24/05/2013 STOREDPROCEDURE 97 404
Class Test 1 Room No1 25/05/2013 DOTNET 86 404
Class Test 1 Room No1 26/05/2013 TAMIL 80 404
Class Test 1 Room No1 23/05/2013 SOCIAL 80 404
Class Test 1 Room No1 27/05/2013 COMPUTER 61 404
************[ Minimum Pass Mark is 35 ]*************
Upvotes: 0
Views: 2702
Reputation: 21
As the lenght of your data is very fluctuating, do not add tabs "\t", this will not work if some data is longer than tabsize
You can use the
String.PadRight(Int32, Char)
Method if you know your maximum expected data lenght.
Remember the usage: the first parameter describes the total lengt not the number of characters to fill
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.71).aspx
Upvotes: 0
Reputation: 216293
Do not use TABs to align the column but use Composite Formatting through AppendFormat method of the StringBuilder
Just as an example for your first column.
I suppose that ExamName
should be inserted left aligned in a column large 20 characters.
You could write
paragraph.AppendFormat("{0:20}","ExamName");
while for the Grade
column right aligned in a 6 char space
paragraph.AppendFormat("{0:-6}\r\n", "Total");
Of course this kind of alignement is dependent on the kind of font used in the RichTextBox. If you use a proportional font there is no way to align this text in the way you like.
At the end I really suggest you to use a DataGridView instead
Upvotes: 3