我通过以下代码在列表框中加载数据库:
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"]);
}
}
}
结果:
如何在列表框中对齐这样的数据?
答案 0 :(得分:1)
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"]);
}
}
}
这对你有用。请注意,“int numberOfSpaces = 30 - lastNameLength - idLength;”行中的30,30只是一个硬编码值,表示可以放在文本框中单行的字符数。您可以使用该号码播放或从文本框中提取该号码。但是这个方法现在应该设置框的左边的姓,id应该固定在框的右边。我希望这会有所帮助。
此致
凯尔
答案 1 :(得分:0)
我认为您应该在dataGridView组件中添加数据并使用表事件。
阅读有关Datagridview的this文章,直至您的描述是您需要的
答案 2 :(得分:-1)
$.ajax({
url:"<?php echo base_url();?>products/basket",
method:"POST",
success:function(data)
{
alert("Are you sure?");
window.location.href="<?php echo base_url();?>users/basket";
}
});