【问题标题】:Pulling & Splitting up database items in a listview在列表视图中提取和拆分数据库项目
【发布时间】: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


【解决方案1】:

第一次使用 Listview,所以忘了使用项目和子项目。这是我为其他苦苦挣扎的人完成的方法:

public void populateListView()
            {
                lbxBugged.Items.Clear();
                SqlCeCommand cm = new SqlCeCommand("SELECT Bug_ID, Bug_Code, Bug_Description, Bug_Author FROM tblBugs ORDER BY Bug_ID ASC", mySqlConnection);

                try
                {
                    mySqlConnection.Open();
                    SqlCeDataReader dr = cm.ExecuteReader();
                    while (dr.Read())
                    {
                        ListViewItem item = new ListViewItem(dr["Bug_ID"].ToString());
                        item.SubItems.Add(dr["Bug_Code"].ToString());
                        item.SubItems.Add(dr["Bug_Description"].ToString());
                        item.SubItems.Add(dr["Bug_Author"].ToString());

                        lbxBugged.Items.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                }


            } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多