我有一个像账单的数据, ID |项目|总价 我需要像桌子一样打印它们,但是使用richtextbox。 我找到了一些代码,但问题是我必须从右到左对齐数字,从左到右对齐项目文本。
像这样ID Item TotalPrice
1 Monitor TV 10 cm 100.50
1.1 HP DESKJET 3630 /3636 MULTIFUNKTIONS WIFI DRUCKER 49.50
1.2 Canon PIXMA MG5750 Tintenstrahldrucker Multifunktionsgerät 99.60
2 Dell UltraSharp U2715H 69 cm (27 Zoll) 16:9 610.82
3 15.6' (39,62cm) HP 250 G5 I5/8GB/1TB/W10Home 508.02
但我得到了这个:
ID Item TotalPrice
1 Monitor TV 10 cm 100.50
1.1 HP DESKJET 3630 /3636 MULTIFUNKTIONS WIFI DRUCKER 49.50
1.2 Canon PIXMA MG5750 Tintenstrahldrucker Multifunktionsgerät 99.60
2 Dell UltraSharp U2715H 69 cm (27 Zoll) 16:9 610.82
3 15.6' (39,62cm) HP 250 G5 I5/8GB/1TB/W10Home 508.02
foreach (DataRow dr_pos in GenerateData().Rows)
{
richTextBox1.Text += dr_pos[0].ToString().PadRight(5, ' ')
+ dr_pos[1].ToString().PadLeft(0, ' ')
+ dr_pos[2].ToString().PadLeft( 40 , ' ') ;
richTextBox1.Text += Environment.NewLine;
}
private DataTable GenerateData()
{
DataTable table = new DataTable();
for (int i = 0; i < 3; i++)
{
table.Columns.Add("Column " + i.ToString(), typeof(string));
}
{
// Header
DataRow dr = table.NewRow();
dr[0] = "ID";
dr[1] = "Item";
dr[2] = "TotalPrice";
table.Rows.Add(dr);
// Data 0
dr = table.NewRow();
dr[0] = "1";
dr[1] = "Monitor TV 10 cm";
dr[2] = "100.50";
table.Rows.Add(dr);
// Data 1
dr = table.NewRow();
dr[0] = "1.1";
dr[1] = @"HP DESKJET 3630 /3636 MULTIFUNKTIONS WIFI DRUCKER ";
dr[2] = "49.50";
table.Rows.Add(dr);
// Data 2
dr = table.NewRow();
dr[0] = "1.2";
dr[1] = @"Canon PIXMA MG5750 Tintenstrahldrucker Multifunktionsgerät";
dr[2] = "99.60";
table.Rows.Add(dr);
// Data 3
dr = table.NewRow();
dr[0] = "2";
dr[1] = @"Dell UltraSharp U2715H 69 cm (27 Zoll) 16:9 ";
dr[2] = "610.82";
table.Rows.Add(dr);
// Data 4
dr = table.NewRow();
dr[0] = "2";
dr[1] = @"15.6' (39,62cm) HP 250 G5 I5/8GB/1TB/W10Home";
dr[2] = "508.02";
table.Rows.Add(dr);
return table;
}
}
我阅读了许多文章和解决方案,但他们并没有按照我的意愿工作。
提前致谢
答案 0 :(得分:1)
以下示例演示了我在上述评论中提供的原则:
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var table = GenerateData();
var maxNameLength = table.AsEnumerable().Max(row => ((string) row[1]).Length);
var formatSpecifier = "{0,-5}{1,-" + (maxNameLength + 2) + "}{2,10}\r\n";
foreach (DataRow row in table.Rows)
{
richTextBox1.AppendText(string.Format(formatSpecifier, row[0], row[1], row[2]));
}
}
private DataTable GenerateData()
{
DataTable table = new DataTable();
for (int i = 0; i < 3; i++)
{
table.Columns.Add("Column " + i.ToString(), typeof(string));
}
// Header
DataRow dr = table.NewRow();
dr[0] = "ID";
dr[1] = "Item";
dr[2] = "TotalPrice";
table.Rows.Add(dr);
// Data 0
dr = table.NewRow();
dr[0] = "1";
dr[1] = "Monitor TV 10 cm";
dr[2] = "100.50";
table.Rows.Add(dr);
// Data 1
dr = table.NewRow();
dr[0] = "1.1";
dr[1] = @"HP DESKJET 3630 /3636 MULTIFUNKTIONS WIFI DRUCKER ";
dr[2] = "49.50";
table.Rows.Add(dr);
// Data 2
dr = table.NewRow();
dr[0] = "1.2";
dr[1] = @"Canon PIXMA MG5750 Tintenstrahldrucker Multifunktionsgerät";
dr[2] = "99.60";
table.Rows.Add(dr);
// Data 3
dr = table.NewRow();
dr[0] = "2";
dr[1] = @"Dell UltraSharp U2715H 69 cm (27 Zoll) 16:9 ";
dr[2] = "610.82";
table.Rows.Add(dr);
// Data 4
dr = table.NewRow();
dr[0] = "2";
dr[1] = @"15.6' (39,62cm) HP 250 G5 I5/8GB/1TB/W10Home";
dr[2] = "508.02";
table.Rows.Add(dr);
return table;
}
}
}
我测试了那些确切的代码,它对我有用。
答案 1 :(得分:1)
我认为你应该在第二个领域使用PadRight。只有一个padleft(0),然后数字上的padleft(40)可以是正确的。例如:
richTextBox1.Text += dr_pos[0].ToString().PadRight(5, ' ')
+ dr_pos[1].ToString().PadRight(40, ' ')
+ dr_pos[2].ToString().PadLeft(5, ' ') ;
顺便说一下,你必须为文本框设置等宽字体才能工作。