姓の正確な最大長 (テーブルで設計) を把握し、適切な長さ (+ 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% の幅を最初の「仮想列」の描画に使用し、残りを 2 番目の「仮想列」の描画に使用しました。それをカスタマイズするのはあなたの部分です。