我尝试使用\ t对列表框中的数据进行对齐,但是对于长
的数据不起作用using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
{
SqlDataReader dr = SqlCommand.ExecuteReader();
while (dr.Read())
{
listBox1.Items.Add((string)dr["LastName"] + "\t\t" + dr["ID"]);
}
}
}
结果:
如何在列表框中对齐这样的数据?
出于某些目的,我必须使用listbox而不是使用datagridview或listview。
答案 0 :(得分:2)
你应该知道姓氏(在你的表中设计)的确切最大长度,并应用适当的长度,例如+ 10.这里我使用50(最大长度)用于演示目的。
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
{
SqlDataReader dr = SqlCommand.ExecuteReader();
while (dr.Read())
{
listBox1.Items.Add(dr["LastName"].ToString().PadRight(50) + dr["ID"]);
}
}
}
抱歉,我没有测试它,但正如rene所说,使用固定宽度字体会有所帮助。但是我有另一个使用DrawItem
的解决方案,这是不完整的,但可以帮助你开始,完成它我认为我们需要更多的测试和自定义代码:
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
{
SqlDataReader dr = SqlCommand.ExecuteReader();
while (dr.Read())
{
listBox1.Items.Add(string.Format("{0}\n{1}",dr["LastName"],dr["ID"]));
}
}
}
//DrawItem
//first, set listBox1.DrawMode = DrawMode.OwnerDrawFixed;
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
string[] ss = listBox1.Items[e.Index].ToString().Split(new char[]{'\n'});
Rectangle rect = new Rectangle(e.Bounds.Left, e.Bounds.Top, (int) (e.Bounds.Width * 0.5), e.Bounds.Height);
Rectangle rect2 = new Rectangle((int)(e.Bounds.Width * 0.5), e.Bounds.Top, e.Bounds.Width - (int)(e.Bounds.Width * 0.5), e.Bounds.Height);
StringFormat sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center };
e.Graphics.DrawString(ss[0], listBox1.Font, new SolidBrush(listBox1.ForeColor), rect,sf);
e.Graphics.DrawString(ss[1], listBox1.Font, new SolidBrush(listBox1.ForeColor), rect2, sf);
}
正如我所说,这只是为了让你开始,而不是完整和完美的代码使用。例如,我使用50%宽度的列表框来绘制第一个“虚拟列”,剩余部分用于绘制第二个“虚拟列”。这就是你自己定制的一部分。
答案 1 :(得分:1)
如果您使用ListView,则可以更灵活地使用列:
// initialize once (use the designer)
ListView lv = new ListView
{
Top = 200,
Left = 10,
Width = 300,
Height = 300,
View = View.Details // this does the trick for multiple columns
};
// add two Columns in the designer
lv.Columns.Add(
new ColumnHeader {Name = "ch1", Text = "Lastname"});
lv.Columns.Add(
new ColumnHeader { Name = "ch2", Text = "Id" });
this.Controls.Add(lv);
// once you have that you can add ListViewItems to the view
using (var myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (var SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
{
var dr = SqlCommand.ExecuteReader();
while (dr.Read())
{
// listBox1.Items.Add((string)dr["LastName"] + "\t\t" + dr["ID"]);
lv.Items.Add(new ListViewItem(new string[] { dr["LastName"], dr["ID"] }));
}
}
}