【发布时间】:2015-12-02 16:20:26
【问题描述】:
我编写了一些代码来简单地从紧凑型 SQL 服务器 (4.0) 中提取数据库信息。此刻,我刚刚将检索到的每个项目用几个空格分开,但我想知道如何拉出每个项目并使其与标题相对应。在这里初始化:
初始化列表视图
private void InitializeListView()
{
// Set the view to show details.
lbxBugged.View = View.Details;
// Allow the user to rearrange columns.
lbxBugged.AllowColumnReorder = true;
// Select the item and subitems when selection is made.
lbxBugged.FullRowSelect = true;
// Display grid lines.
lbxBugged.GridLines = true;
// Sort the items in the list in ascending order.
lbxBugged.Sorting = SortOrder.Ascending;
// Attach Subitems to the ListView
lbxBugged.Columns.Add("Code", 300, HorizontalAlignment.Left);
lbxBugged.Columns.Add("Description", 200, HorizontalAlignment.Left);
lbxBugged.Columns.Add("Author", 120, HorizontalAlignment.Left);
}
我试过了:
最初使用 Listbox,但我已经读过,对于多列,必须使用列表视图。所以我换成了一个列表视图,但它没有填充列表。不太确定哪种方法最好。
我已经初始化了列表视图,所以它显示了标题,我只需要知道如何用相应的数据库信息填充这些空间。
populateListBox(从我使用列表框开始,一直在研究,但只能找到使用来自实际数据库而不是紧凑型数据库的数据库信息填充列表视图的人。)
public void populateListBox()
{
String query = "SELECT Bug_Code, Bug_Description, Bug_Author FROM tblBugs";
SqlCeCommand mySqlCommand = new SqlCeCommand(query, mySqlConnection);
try
{
mySqlConnection.Open();
SqlCeDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
lbxBugged.Items.Clear();
while (mySqlDataReader.Read())
{
lbxBugged.Items.Add(mySqlDataReader["Bug_Code"].ToString() + " " + mySqlDataReader["Bug_Description"].ToString() + " " + mySqlDataReader["Bug_Author"].ToString());
}
}
catch (SqlCeException)
{
MessageBox.Show("Error populating list box");
}
}
【问题讨论】:
-
ListViewItem 对象有一个 SubItems 集合属性。
-
我将如何使用 SQL compact 执行此操作?
-
我不确定为什么 SQL 紧凑部分是个问题。
var lvi = new ListViewItem(new string[] { rdr["Bug_Code"].ToString(), rdr["Description"].ToString(), etc.}); -
我设法编写了一个新方法并将其整理出来。我会将其发布为答案。谢谢
-
谢谢 LarsTech - 帮了很多忙 :)
标签: c# sql database listview listbox