C# 对齐列表框中的数据
我通过以下代码在列表框中加载数据库:C# 对齐列表框中的数据,c#,winforms,ado.net,listbox,text-alignment,C#,Winforms,Ado.net,Listbox,Text Alignment,我通过以下代码在列表框中加载数据库: using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString)) { myDatabaseConnection.Open(); using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName f
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"] + " " + dr["ID"]);
}
}
}
结果:
如何在listbox中像这样对齐数据
我认为您应该在dataGridView组件中添加数据,并处理表事件 阅读关于Datagridview的文章,根据您的描述,使用(SqlConnection myDatabaseConnection=new SqlConnection(myConnectionString.ConnectionString))它是您所需要的
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())
{
int lastNameLength = dr["LastName"].Length;
int idLength = dr["ID"].Length;
int numberOfSpaces = 30 - lastNameLength - idLength;
string spaces = string.Empty;
for (int i = 0; i < numberOfSpaces; i++)
{
spaces = spaces + " ";
}
listBox1.Items.Add((string)dr["LastName"] + spaces + dr["ID"]);
}
}
}
{
myDatabaseConnection.Open();
使用(SqlCommand-SqlCommand=new-SqlCommand(“从员工中选择ID、姓氏”,myDatabaseConnection))
{
SqlDataReader dr=SqlCommand.ExecuteReader();
while(dr.Read())
{
int LastName Length=dr[“LastName”].Length;
int idLength=dr[“ID”]长度;
int numberOfSpaces=30—lastNameLength—idLength;
字符串空间=string.Empty;
for(int i=0;i凯尔
\t
应该能做到这一点。这里的另一个答案是:不包含“Lenght”的定义,并且找不到接受“object”类型的第一个参数的扩展方法“Lenght”,请正确格式化代码,并可能添加一些解释
while (dr.Read())
{
listBox1.Items.Add((string)dr["LastName"] + " " + dr["ID"],HorizontalAlignment.Right);
}